How to kill the application using the home button

I want to kill my application when the user clicks the home button in the middle of the application.

And also someone came up to me to use the method onUserLeaveHint(). where should i use this method in my appilcation?

+3
source share
3 answers

In the old version for Android, this worked. But Android changed that because "The main button should stay on the main button" and they don’t want anyone to redefine the Home button. And for this reason, your code will not work.

maybe you can implement this:

@Override
protected void onStop() {
    super.onStop();
    finish();
}

, , onStop() , (, , ), finish Activity, .

?

+2

onPause() :

@Override
public void onPause() {
    super.onPause();
    this.finish();
}

, Android "" .

+2

The Home button is a very dangerous button for overriding, and because of this, Android will not allow you to redefine its behavior in the same way you do the BACK button. Thus, you cannot permanently redefine the Home button without user confirmation (to ensure security) Therefore, even when using:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
    if(keyCode == KeyEvent.KEYCODE_HOME)
    {
        Log.d("Test", "Home button pressed!");
    }
    return super.onKeyDown(keyCode, event);
}

it will not work because KeyEvent.KEYCODE_HOME is always processed by the system and never sent to the application.

+1
source

All Articles