How to finish the application on HOME
button click?

- 12,111
- 21
- 91
- 136

- 151
- 1
- 2
- 5
5 Answers
You don't - just let Android suspend your app and tidy it up when necessary.

- 5,923
- 4
- 38
- 47
You should only be finishing the Activity by detecting the click and calling finish() on the activity.

- 21,962
- 20
- 69
- 98
As already mentioned before you really should consider NOT using this approach to finish your application.
Anywho: Here is some code you can use to detect Home-Button pushes and call appropriate functions.
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_HOME:
finish();
return true;
}
}
return super.onKeyDown(keyCode, event);
}

- 1,060
- 1
- 10
- 25
-
This also detects the software home button afaik – Johnnycube Sep 10 '13 at 15:11
You can set the intent you used to start acitvity with flag FLAG_ACTIVITY_NO_HISTORY, and according to the doc:
public static final int FLAG_ACTIVITY_NO_HISTORY
If set, the new activity is not kept in the history stack. As soon as the user navigates away from it, the activity is finished. This may also be set with the noHistory attribute. Constant Value: 1073741824 (0x40000000)
This might fit the use case.

- 214
- 3
- 7
Android did't gave permission to programmers to handle home button for user convenience. when user wants sudden exit from the application he will press the home button.

- 4,291
- 2
- 26
- 45