My app history is normal; I am not using any fancy flags or anything when I am launching intents. Only sequential, single-task launched activities. If I go Home, and then use a task killer to kill my app, the next time I open the app it starts up at the last activity that I was on before the kill rather than the declared Main activity. Additionally, if I then hit Back to exit the app and reopen it again, I get whatever Activity was open before the one that I had previously opened before the kill.
Something like this:
A = Main Activity; B = Some Other Activity; C = Some Other Activity
I would expect that the backstack would not be remembered after the task kill. I would expect that the app would always start from the declared Main activity unless it is already running in the background. (which is not the case here since the kill is being utilized) Both of these things seem not to be happening however and I do not know why.
I tried putting in some code to set an "initialized" boolean in my Application class on A's onCreate and then to check against it in B and C's onCreate. The idea there was to launch A's intent with the NEW_TASK and CLEAR_TOP flags to try to detect and correct the scenario, but it still exhibited the same exact behavior except that it would also put A on top of whatever was leftover. So instead of starting the app into B, it would start the app into BA.
Based on everything I have read about activity lifecycle and OS killing of apps, this is impossible, but it is definitely happening on all of my test devices. Does anyone have any ideas as to what might be causing this odd behavior?
Your app is behaving normally. This is the default behaviour. Android will kill off an app if it is in the background and another app needs resources. When the user returns to the original app, he will expect to return to it in the same state it was when he left it, so Android remembers the state and restores it. That is the standard, default, user-expected behaviour.
If you don't want this behaviour, you should add
android:clearTaskOnLaunch="true"
to the <activity> declaration for your root Activity (the one with ACTION=MAIN and CATEGORY=LAUNCHER) in the manifest. This will cause Android to start your app from scratch when a user returns to it from the HOME screen.
If you also don't want users to return to your app's previous state when launched from the "Recent Tasks" screen, you'll want to add
android:excludeFromRecents="true"
to the <activity> declaration for your root Activity as well. This will prevent Android from adding your app to the list of recent tasks, so users cannot return to your app from the "Recent Tasks" screen.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With