0

I'm newbie for Android Studio & Java, i'm PHP user. Just want to know how to do like PHP session in Android Studio. No matter which Activity i go to, i can easily get the session value example like the User ID. (session["USERID"])

now the method i using is putting extra everytime i call for another activity. i'm sure there will be a better way to do this.

anyone have any good suggestion?

PS: I google around and it keep return me PHP session tutorial/example/etc but not for Android Studio....(may be i enter work #keyword or sentence)

Thank You Very Much


Thanks to fillobotto & Arshid KV

here is my code

first_main activity

sharedpreference = getSharedPreferences(BIZInfo, Context.MODE_PRIVATE);
sharedpreference.edit().putString(userid, "12345");
sharedpreference.edit().commit();

second_main activity

sharedpreference = PreferenceManager.getDefaultSharedPreferences(this);
String restoredText = sharedpreference.getString("text", null);
if (restoredText != null) {
   sp_name = sharedpreference.getString("userid", "No name defined");
}
Log.i("TAG", "onCreate: [" + sp_name + "]");

log show empty value/nothing...

what went wrong!?

JiiN Wee
  • 45
  • 1
  • 7
  • Maybe [SharedPreferences](http://stackoverflow.com/questions/3624280/how-to-use-sharedpreferences-in-android-to-store-fetch-and-edit-values) could do the trick. Is this what you meant? – fillobotto Apr 03 '16 at 18:38
  • i think i JUST try this but i dont know how to retrieve it from another activity... any good and easy to understand reference? – JiiN Wee Apr 03 '16 at 18:42

2 Answers2

0

You can use SharedPreferences as session in php

Demo code :-

Setting values in Preference:

SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
 editor.putString("name", "Arshid");
 editor.putInt("Age", 22);
 editor.commit();

Retrieve data from preference:

SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE); 
String restoredText = prefs.getString("text", null);
if (restoredText != null) {
  String name = prefs.getString("name", "No name defined");//"No name defined" is the default value.
  int Age = prefs.getInt("Age", 0); //0 is the default value.
}
Arshid KV
  • 9,631
  • 3
  • 35
  • 36
0

You were really near to the solution. This is what I use:

public static String getSession(Activity context) {
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
    return sharedPreferences.getString("session", null);
}

public static void setSession(Activity context, String session) {
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putString("session", session);
    editor.apply();
}

I'm actually passing the Activityto get SharedPreferences, but in this way you will obtain an instance of the object which is not activity-related.

fillobotto
  • 3,698
  • 5
  • 34
  • 58