Hi I have application with more than 20 activities.I want to close my app when Home button is pressed.
-
What do you mean by closing your app? The home button triggers the onStop() event of the current activity, if you want to do special things when going home put them there! – Amokrane Chentir Mar 11 '11 at 23:08
-
1Yeah it triggers onStop().But onStop() is also trigged when I navigate from one activity to another.So I can't put System.exit() in onStop() – Altaf Mar 11 '11 at 23:16
-
You don't want System.exit(). See my answer below. – J. Taylor Mar 11 '11 at 23:32
-
I have want to keep it in background I just terminate the app – Altaf Mar 11 '11 at 23:36
-
You don't need to worry about terminating the App. Android will take care of that whenever it needs the resources. See my answer below. In particular, read about the Activity Lifecycle (I posted a link to docs on it in the answer). – J. Taylor Mar 11 '11 at 23:38
-
I have already read it,but It doesn't provide any solution.Android terminate the app after some time interval.but if the use again launch the app in few seconds he will get the same state of App. – Altaf Mar 11 '11 at 23:42
-
Yes, it does provide a solution to your problem. I specifically told you how to make sure that they DON'T get the same state of the App. (Read the part about setting noHist = "True"). – J. Taylor Mar 12 '11 at 01:39
5 Answers
There is no notion of "close my app" in Android. Android will get rid of your activities, followed by your process, after some period of inactivity by the user.

- 986,068
- 189
- 2,389
- 2,491
-
Button I have clear all the session of my application when my app is background.When someone pressed home and after few second he returns all the sessions my application remains unchanged. – Altaf Mar 11 '11 at 23:10
-
@Altaf: "I have clear all the session of my application when my app is background. When someone pressed home and after few second he returns all the sessions my application remains unchanged." -- if the user returns to your application, and they have been gone longer than some particular timeout period, clear your session (whatever objects those are), and route the user back to the starting point using `startActivity()` with an `Intent` using `FLAG_ACTIVITY_CLEAR_TOP` and `FLAG_ACTIVITY_SINGLE_TOP`. – CommonsWare Mar 11 '11 at 23:27
You could use the launchMode
and clearTaskOnLaunch
flags on your root activity from your AndroidManifest.xml:
android:launchMode="singleTask"
android:clearTaskOnLaunch="true"
When you again start your app, all activities will be killed.

- 17,833
- 13
- 59
- 112

- 815
- 11
- 17
You don't want to do System.exit() -- that's not how the Android Activity Lifecycle normally works (read this also).
What you should do is move the App to the background with moveTaskToBack(). Android will then keep your app running in the background, and kill it if it's unused and something needs its resources later.
If you want it to close all of the open Activities when your App is no longer visible, you can set noHist = "True" for all of the child activities (leave the main activity with noHist = "False", though). This will make it where instead of reopening your application on the last Activity they were on, it will open it on the "main" activity (i.e. it will be as if they just restarted the app).
Anyhow, read through the following answers for more information: Close application and launch home screen on Android
I have the same problem. Im writing a banking app and am required, by contract, to log off the user (or exit) when the app is put into background. There are obvious security concerns there.
There are a couple of ways Im looking to do this: 1. Intercept home button (and back button for the root activity) key press events to call logoff and/or finish() 2. In the onStop() method, for every activity, detect whether the activity is being stopped due to a new activity being show - if not, assume app is being put to background so logoff and/or finish()
The first may not work if a notification is brought to the front then the user clicks home (I havent investigated yet). Or maybe there are other ways for an app to be put into the background without pressing these buttons
The second way sounds messy & difficult to maintain
Id welcome any other ideas Drapes
I know android has been designed this way, but it seems naive to think that apps wouldnt want an applicationOnStop event

- 31
- 2
Hi guys what I understood is that u need to know when app goes in background and how to detect it and if I am wrong plz correct me----
The user can go in background if ur app does not provide any way by pressing Back key or Home Key.
You need to use methods "dispatchKeyEvent(KeyEvent event)" to get home key event or back key event and after getting the event you can execute your task.
you can even restrict user from pressing any key but u can not control the home key.

- 23,865
- 10
- 55
- 63