Retaining android app state using alwaysRetainTaskState and lauchMode
I solved this by adding the screenless DispatcherActivity
and making it the default one (by using the very same intent filter). In its onCreate
method you create and call the Intent based on some reasonable default (your Main activity for example) OR based on some saved token that identifies which Activity should be started. That token is saved/refreshed in onStop
method of any Activity you want to call on restart. You can save this token to Preferences.
The rational here is that last activity that was visible will execute onStop method when interrupted.
Word of caution here: I did implement this pattern and it worked reasonably well. However it seems not play too well with history and finally I just gave up and yanked this code out. Nobody complained so far.
For anyone coming here with similar problems, I found something strange that might be what you are seeing... maybe.
Say I have an app with activities A -> B -> C etc. I was having issues with my app always "resuming" to A if it was launched from the app list aka launcher. Resuming from the "resents" screen (long home press) would exhibit correct resume behaviour though (resume to B or C as expected). My manifest was nothing special, I have alwaysRetainTaskState="true" set in my root activity, and launch mode is default (standard).
I was loading the apk onto my phone via a website. After downloading and installing, I would press "Open" to launch the app right away. For some reason (after uninstalling the app) I tired downloading again, installing, but then I pressed the "Done" button instead. Then Launching the app from the launcher/"all apps" list has the same resume behaviour as resuming from recents - in other words my problems were being caused somehow because of the installation process when clicking "Open" instead of "Done".
I verified this "solution" on API10 (2.3.5) and API15 (4.0.4)
FYI singleTask
is not what you want, since it starts a new task:
http://developer.android.com/guide/topics/manifest/activity-element.html#lmode
How are you launching Activity B? Any non-standard launch modes or Intent flags?