I have a login screen and you want the application to appear as if it remained "logged in" on the internal screen after closing the application / destruction / phone call / etc.
I have a Preferences object to store the values following Login or Register . I read preference values in all onResume() screen methods.
After login (for example):
SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(activity); SharedPreferences.Editor editor = app_preferences.edit(); editor.putString("sessionId", application.currentSessionId); editor.putString("userId", application.currentUserId); editor.putString("userEmail", application.currentUserEmail); editor.putString("siteUserId", application.currentSiteUserId); editor.commit();
Inside onResume () actions: (i.e. in internal screens)
SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(activity); application.currentSessionId = app_preferences.getString("sessionId", ""); application.currentUserId = app_preferences.getString("userId", ""); application.currentUserEmail = app_preferences.getString("userEmail", ""); application.currentSiteUserId = app_preferences.getString("siteUserId", "");
Note. I have "global" application variables, i.e. application.currentSessionId , you can just replace your variables
Try something like this, you may not save or return values correctly, because SharePreferences should work
source share