3

I want to kill my app when the user presses the home button in the mid of the application.

And also some one approached me to use the onUserLeaveHint() method. where should i use this method in my appilcation?

Madhan Devan
  • 147
  • 3
  • 10

3 Answers3

2

On older Android version this was working. But Android changed this, because "Home Button should stay Home Button" and they don't want that anybody override the Home Button. And because of this reason your code will not work.

perhaps you can implement this:

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

But i bet you dont wanna do that cause the onStop() is called whenever you app goes to background (e.g. an incoming call while your app is in the foreground) and that will finish the current Activity which was not desired at the moment.

You might wanna have a look at

How to finish an android application?

Community
  • 1
  • 1
SMR
  • 6,628
  • 2
  • 35
  • 56
2

You could try overriding the onPause() button as follows:

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

Keep in mind that Android is intentionally designed to prevent the home button from killing the app.

ucsunil
  • 7,378
  • 1
  • 27
  • 32
  • if someone calls while our app is running the app will be terminated in the middle of something. – SMR Feb 20 '14 at 07:49
1

The Home button is a very dangerous button to override and, because of that, Android will not let you override its behavior the same way you do the BACK button. So you can't permanently override the Home button without the user confirming it (It's for security issue) So 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 since KeyEvent.KEYCODE_HOME is always handled by system and is never sent to application.

Nabz
  • 390
  • 2
  • 14
  • @ ANdroidNabz so you are saying its not possible to make my app to start from the first activity always when i close the app by using the home button ?? – Madhan Devan Feb 20 '14 at 10:54