0

What is the best way to maintain session information across multiple activities? The question What is the best practices on Android to keep data between activities deathes/restarts for the whole application session? has some ideas, but does not really help me.

*My application has multiple activities with the manifest tag singleTop. They essentially work as different tabs - they each maintain their own set of fragments and back stack and so putting it all into one activity would break navigation for the user.

I am currently saving the session data as a static singleton created by my Application subclass. This works fine most of the time, except when the entire application is killed by the OS to save memory (as mentioned in the above link), say, when the user gets a call on a device with low RAM. The only notification the app has that it is going to be killed (as far as I know) is

Activity.onSaveInstanceState(Bundle outState)

So the problem is this: onSaveInstanceState will eventually be called on every activity, not just the top-most one (the one that will appear when the user eventually returns to my app). When each non-top activity resumes, I could use Activity.onRestoreInstanceState(Bundle savedInstanceState) to restore my singleton session, but non-top activities would have old copies of the data (they may have been navigated-away from long before the user got the call).

One solution would be to only restore data to the singleton session Only if it is currently empty, but this relies on the first activity to receive Activity.onRestoreInstanceState being the top activity. This is not always the case - if the user gets a call and then returns to the app via the launcher icon, then the Main activity will be resumed first and brought forward, not the activity the user was on when they got the call.

A simple notification in the Application class that the application is being killed by the OS (not the user) is really what I need - I would then save the session to a file, and read it back on the first call to Activity.onRestoreInstanceState, but AFAIK this doesn't exist.

Community
  • 1
  • 1
rockgecko
  • 4,127
  • 2
  • 18
  • 31
  • 1
    What is the exact problem you are facing. You can save instance and you can also return bundle onactivityresult... – Bharat Sharma Sep 12 '12 at 04:14
  • The problem is determining **which** resuming activity has the latest data – rockgecko Sep 12 '12 at 04:28
  • I think activity on top will have latest data if you are not using any thread or something like that. You can send latest data from child to parent.... – Bharat Sharma Sep 12 '12 at 04:36
  • No, as I said in the question, if you are in some activity and get a call (or just press home button), and then come back by pressing the launcher icon (rather than finding the app in the recent tasks popup), then the Main activity is the first to resume, **not** the last one you were on. – rockgecko Sep 12 '12 at 04:57

2 Answers2

3

If you have some data that you want to store when app closes and reload them when app starts, you can have a mechanism to store and retrieve them on application's shutdown and startup. To do this override onStop() and onStart() methods on Activity's lifecycle.

enter image description here

And I think the best way to store and retrieve data in these two methods is SharedPreferences.

Ali Behzadian Nejad
  • 8,804
  • 8
  • 56
  • 106
0

I would simply insist to use Application class to save the session. Using Application class you will be able to access the session everywhere were you are having Context so probably you can access in your whole Application. Application class also maintains the value after you Application is closed so its better to clear previous session when your Application is re-launched.

As the main use of Application class is to maintain global state of variables so that they can be used throughout the Application with updated values.

Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242
  • Yes, I **am** storing my session data in Application (as a singleton). The problem is Application can be killed in some low memory situations, but the activity can still be resumed later. – rockgecko Sep 12 '12 at 05:00
  • You can override `onLowMemory()` method and update the user about low memory and manage the Applicaton state. – Lalit Poptani Sep 12 '12 at 05:05
  • Thanks for the suggestion, unfortunately, it isn't called (at least on the phone I'm testing on). The OS just kills the process without further warning :( – rockgecko Sep 12 '12 at 05:36