I am making an app and I am putting the final touches on it and i was wondering, if the user presses the "home" button while the application is running, then clicks on the icon to try to run it again, what will the app do? Will it bring the current app to the front? Will it run a new instance? I am wondering because my app has a lot of threads and if the user was running 2 instances of my app it would kill their battery life.
Asked
Active
Viewed 2,217 times
3
-
as you can see [here](http://developer.android.com/reference/android/app/Activity.html) there are guideline on activity – Jayyrus Feb 25 '12 at 18:02
-
is what I'm looking for onStop()? – John Feb 25 '12 at 18:04
-
ondestroy is called when home button is clicked – Jayyrus Feb 25 '12 at 18:07
-
1@JackTurky this is not correct. `onDestroy()` is NOT called when the HOME button is clicked. – David Wasser Jun 27 '13 at 19:09
-
@DavidWasser is correct, onDestroy() will only be called if the android system kills the process to free up memory, onPause() will get called when the home button is pressed. – John Jun 27 '13 at 19:11
-
@John No, that's not correct either. If Android kills the app's process to free up memory, it just does that. It kills the process. Boom. Dead. It doesn't call **anything**, including `onDestroy()`. This is why you need to save state in `onPause()`, because that is the **only** lifecycle method that is guaranteed to be called. – David Wasser Jun 27 '13 at 19:21
-
@DavidWasser see http://stackoverflow.com/questions/4449955/ondestroy-never-called/4449988#4449988 also http://developer.android.com/reference/android/app/Activity.html#onDestroy%28%29 says that the onDestroy method will be called when the system is preparing to kill the app – John Jun 27 '13 at 19:39
-
The referenced StackOverflow answer is also wrong :-( The developer doc says _"...because the activity is finishing ... or because the system is temporarily destroying this instance of the activity..."_. `onDestroy()` **will** be called if Android is killing off an **`Activity`**, which in fact, it only does during a configuration change. The same doc also says _"There are situations where the system will simply kill the activity's hosting process without calling this method (or any others) in it"_ and that's what happens when it kills the **process**. – David Wasser Jun 27 '13 at 20:02
-
Sorry to be so pedantic about this, but a lot of developers don't really understand how this works. And it is complicated and confusing, I know. – David Wasser Jun 27 '13 at 20:04
1 Answers
2
It depends on the android:launchMode
of your activity, set in the Manifest.
With the default setting the system always creates a new instance of the activity in the target task and routes the intent to it.
See the reference for detailed info.

Mariusz Jamro
- 30,615
- 24
- 120
- 162
-
so i want to set my activity to single_instance in order to prevent multiple instances? – John Feb 25 '12 at 18:08
-
`singleTask` i believe. With `singleInstance` the activity is always the single and only member of its task. BTW. are you sure you need to prevent multiple instances? The default behavior is enough for most of cases. – Mariusz Jamro Feb 25 '12 at 18:14
-
Well my app tends to use a lot of memory, multiple instances would be bad for battery life and performance – John Feb 25 '12 at 18:19
-
1This answer is not correct. If a user is using your app and presses the HOME button, the task containing your activities is put in the background. If the user then returns to your app (either by selecting it from the list of recent tasks, or by tapping on your app's icon int he list of available applications), Android will simply bring the existing task containing your activities back to the foreground. Android will NOT create a new instance of your launch activity in this case. – David Wasser Jun 27 '13 at 19:13
-
1Please note, however, that there is a long-standing Android bug that indeed causes a new instance of the root activity to be created in certain special cases. This can happen if the app is launched just after the app is installed/updated from the installer. But this is a special case and not the default behaviour. See http://stackoverflow.com/a/16447508/769265 – David Wasser Jun 27 '13 at 19:15