What to do when:
1. The app uses a complex structure of Activities and Fragments
2. I return to a backgrounded application that has been (partially or not) destroyed in the meantime
3. It returns me to the last screen, which lies deep in the workflow
4. I need it to return to the first screen to reinitialize things (because it's complex and requires user interaction)
Just to be clear:
I am asking how to deal with the existing stack(s) of Activities and Fragments upon encountering this situation.
- action to take: launch an Intent? Just finish? Do something to held substructures and then finish?
- when to perform it - immediately in both Activity.onCreate and Fragment's empty constructor? Or are Activities sufficient?
- How best to detect it: all I've come up with so far is: binding to a Service that holds the necessary data/connection, and then asking if it's been initialized. But the binding finishes AFTER onResume.
I am not asking about anything UI-related.
I am not asking for a solution that will only work for one specific application, so don't ask for code.
If you see this question as vague, here is a one-sentence version: "How to dismiss the stack and return to the first screen?"
For details (as much, at least, as are relevant), see https://stackoverflow.com/questions/14650342.
-
All right, FLAG_ACTIVITY_CLEAR_TOP is at least part of *what to do*. The rest crashes upon a lack of consensus about what exactly is and is not done by Android to an app's Activities. I'd sure love to learn that some precautions are indeed unnecessary, but no-one can offer certainty in that regard, it seems... – kaay Feb 05 '13 at 09:00
3 Answers
If you recognize that your application is in an inconsistent state and you need to start over, the easiest way is to relaunch your root activity like this:
Intent intent = new Intent(this, MyRootActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
This will clear all activities from the task and restart the root activity.
I would suggest doing this in onCreate()
of activities.

- 93,459
- 16
- 209
- 274
-
Anup was a minute faster, but this is more complete. What remains: 1. Ignoring the Activity's savedInstanceState, will I *never* need to worry about Fragments? 2. How to detect it? I use a bound Service as a way that (hopefully) allows the shared objects to contain running Threads, and stop them cleanly. Is it guaranteed that, in the background, there is no partial (only some Activities) destruction, and, if at all, the whole process is killed? – kaay Feb 04 '13 at 12:54
-
Android doesn't selectively kill activities. If Android decides that it wants to kill your app, then it simply kills the process. This means that all your components will be killed (activities, services, content providers, etc.). This will only happen when your app is in the background. When the user returns to the app, Android creates a new process and restarts the components as it needs them. You can detect that Android killed your process simply by setting a static (class) variable to "true" in the `onCreate()` of your root activity (for example) and checking it as appropriate. – David Wasser Feb 04 '13 at 13:10
-
Sorry, I'm not sure exactly what you are asking when you say "1. Ignoring the Activity's savedInstanceState, will I never need to worry about Fragments?" – David Wasser Feb 04 '13 at 13:11
-
I meant not passing it to super.onCreate. Am I mistaken believing that Activities that are not visible (like fully hidden by another Activity) can also be killed, unless the rest of the stack had been called with startActivityForResult? – kaay Feb 04 '13 at 13:33
-
No, Android will not kill off hidden activities. The only thing Android will do is to kill the entire process. It doesn't selectively kill off activities (except in the case of configuration change, like an orientation change, where it will kill and immediately recreate a single activity). – David Wasser Feb 04 '13 at 14:25
-
If you always want to restart your app from the beginning if Android kills your process, then you can safely ignore any saved instance states (except in case of a configuration change, like an orientation change) – David Wasser Feb 04 '13 at 14:28
-
These say otherwise: http://developer.android.com/training/basics/activity-lifecycle/stopping.html ("Once your activity is stopped, the system might destroy the instance if it needs to recover system memory. In extreme cases(...) kill your app process"), http://developer.android.com/training/basics/activity-lifecycle/recreating.html ("The system may also destroy your activity if it's currently stopped and hasn't been used in a long time"). They say "Activity" rather than "Application process". They also suggest stopping Threads in onDestroy, but that's a whole 'nuther mistake. – kaay Feb 04 '13 at 15:56
-
Also, I've sometimes witnessed situations where navigating the backstack (of an ActivityGroup, but still) back gave me onCreate(notNull) on low memory, with no config changes. It no longer happened when I used startActivityForResult. So, as you can see, I have reason to doubt your claim that partial destruction occurs neither with the app in background nor in foreground. – kaay Feb 04 '13 at 16:01
-
Thats news to me then. I've never seen it and my understanding is that it doesn't happen. I know the documentation says that it **can**, but my understanding is that it isn't implemented that way. – David Wasser Feb 04 '13 at 16:21
when app is in the background and os wants to free some memory. whole PID is killed, which kills ALL activities. Docs are misleading about this behaviour. See this post
Android app out of memory issues - tried everything and still at a loss
where hackbod (Android platform developer) kind of confirms it.
When you have mulitple activities A,B,C and when your app is killed and you want to bring it back. onCreate(not null) of most top activity will be called. Its worth noticing that when this scenario happens and you do something like
onCreate(not null){
startActivity(new intent)
finish;
two things will happen :
- startActivity will launch new activity
- finish will close current activity BUT it will bring back the previous activity (from killed pid) with its onCreate(not null) which simply was next activity in the activity stack.
This might cause multiple activity instances problems.
I had a similar problem some time ago, ended up with ugly hack but it worked :
to reinit whole app, I added something like
onCreate(not null){
if(launcher activity == null){
finish();
return;
in all activities. Depending on what you want to achieve use flags as others have suggested. Hope that gives you some hints.

- 1
- 1

- 14,034
- 3
- 35
- 49
-
hackbod seems to be the only source confirming that the published docs are wrong. My own tests conducted over a year ago suggest otherwise, though, as I'd sometimes get onCreate(notNull) returning from fullscreen Activities launched for 20 minutes. Still, I'm accepting this answer. – kaay May 24 '13 at 09:19
If your goal is to dismiss all the activities and get back to your home/main activity then you can simply use:
Intent intent = new Intent(getApplicationContext(), Home.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
This will clear all the activities on top of home and return you to the home screen.

- 20,443
- 6
- 51
- 84