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?
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?
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
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.
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.