Recovering from Android app being killed

android

Solution

- You can specify which activities will appear on "Recent apps" and make sure to include only the main `Activity` or any other "safe" `Activity` (`android:excludeFromRecents`)

- You can recognize the "launched from history" case by looking for `FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY` in your launching `Intent`

Problem

What's happening: - I open my app and then press the home button to leave it for a moment. - I go into `Settings > Applications > Manage Applications > Running`, find my app in the list of cached backgrounded apps, and stop it. - Then I hold down the home button and select my app in the list of recent apps, to run it again. [Note: These steps are just my way of forcing the same symptoms that testers are seeing -- they're just going in and out of the app normally, not forcing it shut.] It opens at the activity I left it in, but in a bad state. The first problem was that the memory holding user info was gone (which makes sense if the app is killed). I solved that by making the User class serializable and storing the current user object in `onSaveInstanceState`, then loading it back up in `onCreate`. Similarly, I had to recreate my `ClientConnectionManager` to get networking to work again. The 'reloaded' activity runs just fine and I can navigate to and use newly created activities, but if I instead hit the back button to get back to an activity that was created before I forcefully stopped the app, I get an exception saying that the User class doesn't exist (ClassNotFoundException, apparently when doing an unparcel). Why is this happening? What is the correct way to handle the case when the app is killed yet still knows which activity to go back to upon reopen? What is the name of this state where the app isn't running but isn't completely shut down?

Original source