May Mainting The Session Using Shared Preference In Android
Hi i want to maintain some sessions in my application. May i use shared preference to maintain it? I not then plz suggest me the proper way with the simple example.
Solution 1:
Maybe you should make use of server to maintain sessions. Because as you have suggested here, using shared preference, you are leaving the option of clearing the preferences from memory to the user. So in this case if the user clears your app data, then your concept of sessions fails.
Solution 2:
Yes, you can use the shared preferences.
For example, to save username, password and session ID, you can:
SharedPreferencespref= myContexy.getSharedPreferences("Session Data", MODE_PRIVATE);
SharedPreferences.Editoredit= pref.edit();
edit.putString("User Name", username);
edit.putString("Password", password);
edit.putInt("Session ID", session_id);
edit.commit();
And to get them:
SharedPreferences pref = myContexy.getSharedPreferences("Session Data", MODE_PRIVATE);
username = pref.getString("User Name", "");
password = pref.getString("Password", "");
session_id = pref.getInt("Session ID", 0);
This is just an example, it is better to use string constants instead of just "User Name" etc.
Solution 3:
You're probably looking onSaveInstanceState. It can be used to save the state of a page and restore it later
Post a Comment for "May Mainting The Session Using Shared Preference In Android"