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 1,106 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
1 Answers
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
- 11,691
- 3
- 41
- 77
-
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