Finishing android app on HOME button click

6.9k views Asked by At

How to finish the application on HOME button click?

5

There are 5 answers

0
skaz On

You should only be finishing the Activity by detecting the click and calling finish() on the activity.

0
RivieraKid On

You don't - just let Android suspend your app and tidy it up when necessary.

1
Johnnycube On

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);
}
0
markmarch On

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.

0
Sandeep P On

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.