android save session state across activities
<p>What is the best way to maintain session information across multiple activities?
The question <a href="http://stackoverflow.com/questions/2033046/what-is-the-best-practices-on-android-to-keep-data-between-activities-deathes-re?rq=1">What is the best practices on Android to keep data between activities deathes/restarts for the whole application session?</a> has some ideas, but does not really help me.</p>

<p>*My application has multiple activities with the manifest tag <code>singleTop</code>. 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.</p>

<p>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</p>

<p><code>Activity.onSaveInstanceState(Bundle outState)</code></p>

<p><strong>So the problem is this</strong>: <code>onSaveInstanceState</code> will eventually be called on <strong>every</strong> 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 <code>Activity.onRestoreInstanceState(Bundle savedInstanceState)</code> to restore my singleton session, but non-top activities would have <strong>old</strong> copies of the data (they may have been navigated-away from long before the user got the call).</p>

<p>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, <strong>not</strong> the activity the user was on when they got the call.</p>

<p>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 <code>Activity.onRestoreInstanceState</code>, but AFAIK this doesn't exist.</p>
