Is there a way to define something like a "session" for an android app?
I have an application that launches multiple activities and the user the app going from activity to activity etc.
Of course may switch to another app, take a phone call, stop using the app because he is busy so the app is in the background etc.
Is there an easy and meaningful way to define something like a single session across all these different cases? So that we can tell when a new session starts and possibly store some data for each session?
Asked
Active
Viewed 979 times
7
-
1Take a look into SharedPreferences – Lal Aug 28 '15 at 18:46
-
@Lal:But saving something there, how does it help? E.g. in http a cookie can expire. – Jim Aug 28 '15 at 18:50
-
@Lal:It is not about how to save something. But how to differentiate among sessions – Jim Aug 28 '15 at 18:50
2 Answers
8
You can use SharedPreferences for that..
SharedPreferences wmbPreference1,wmbPreference2;
SharedPreferences.Editor editor;
//wmbPreference1 for Shared Prefs that lasts forever
wmbPreference1 = PreferenceManager.getDefaultSharedPreferences(this);
//wmbPreference2 for Shared Prefs that lasts only just once each time program is running
wmbPreference2 =getApplicationContext().getSharedPreferences("MYKEY",Activity.MODE_PRIVATE);
To save values
SharedPreferences.Editor editor = wmbPreference1.edit();
editor.putString("MYKEY", "12345");
editor.commit();
You can retrieve the values like
String Phonenumber = wmbPreference1.getString("MYKEY", "");
where MYKEY is the keyname by which you can identify the value..
Lal
- 14,893
- 4
- 41
- 68
-
Using `wmbPreference2 ` you can just save the current `user_id` or something like that which identifies the session, which will automatically be deleted after the app is closed. – Lal Aug 28 '15 at 18:52
-
But I have tried the second approach and restarting the APP the value saved persists (but with `Context.MODE_PRIVATE` – Jim Aug 28 '15 at 19:45
-
If so, then edit the value saved in sharedpreference just before you exit..may be in onstop() – Lal Aug 28 '15 at 19:47
-
But is this guaranteed to be called? I thought `ondestroy()` and `onstop()` are not for code that **must** be executed – Jim Aug 28 '15 at 19:48
-
-
-
Yes its guaranteed..meanwhile see the answers [here](http://stackoverflow.com/questions/15353900/shared-preferences-reset-data-when-app-is-force-closed-or-device-is-restarted) – Lal Aug 28 '15 at 20:00
-
Session is the "Process". All activities, broadcast receivers, services live in application process in memory. You may use member variables to track your active session in application class. SharedPreferences is not designed in that way. – Fatih S. Aug 28 '15 at 22:29
-
@FatihSokmen:But how should I achieve this then? Because a member variable of an activity lasts as long as the activity's `onDestroy()` is not called. So if the user navigates forward from the activity the variable is kept. If the user presses back the variable is GCed. I can't define my variable as part of *an* activity as I have multiple activities and in *one* activity I need to use the session. Please check comments in MorrisonChang's answer – Jim Aug 29 '15 at 08:18
-
@Jim User member variables in Application class. Always define a application class in your manifest. As long as process is alive, you can access them via application object. Application scope is the wider and doesn't depend on other components (activity, service, etc.) – Fatih S. Aug 29 '15 at 12:29
-
@FatihSokmen:So if I use this application class, 1) it is not affected by the same issues that e.g. activities have that can be destroyed at any time? 2) Is there a specific pattern? I.e. extend some class and use a specific member variable? – Jim Aug 29 '15 at 20:20
-
@Jim 1 - No, not effected. Application class is alive when process is active in memory. 2 - You may access application object via "context.getApplicationContext()" or via singleton. check out this link: http://stackoverflow.com/questions/9178245/android-application-as-singleton – Fatih S. Aug 29 '15 at 20:32
1
Unfortunately its up to the developer to define what a 'session' is in Android. Since your activity can be backgrounded for a phone call for a minute and then return to foreground - is that part of the same session or two sessions? How about a gap of 20 minutes? You can store a timestamp but you are responsible for 'expiring' and implementing appropriate behavior.
Morrison Chang
- 10,876
- 3
- 38
- 67
-
I understand what you are saying but if I created my self somesort of expiring cookie how would I do it in a meaningful way? E.g. perhaps I could arbitrary say 1 or 20 hours but may be there is some kind of event (?) or something I could use to figure out something meaningful. – Jim Aug 28 '15 at 19:46
-
What is your 'session' cookie for? Define the use case you are trying to solve. If you are using analytics see: http://stackoverflow.com/questions/25062885/flurry-session-data-vs-google-analytics – Morrison Chang Aug 28 '15 at 20:21
-
It will some thing to define a meaningful range. What I want is to show a specific layout to the user, the user can hide/dismiss it and I want to re-display it again on the next session. Not just next time he goes back/forth to the activity – Jim Aug 28 '15 at 21:14
-
When I mean use case I mean why would the user need to interact with it in the 'next session'. Can't it be a preference. If the session was for login and its a timeout for security reasons - that is a use case. – Morrison Chang Aug 28 '15 at 21:24
-
Because I want to show it often, (like a promotion) but not **too** often and annoy the user. So once a "session" should be enough. – Jim Aug 29 '15 at 08:14