I like to know how to prevent an activity from exiting if the user presses the back and the home button?
Asked
Active
Viewed 677 times
2
-
2there's no way to override home button. you can't lock user to stay on your application. it's the same way on all mobile devices. look iPhone, also no way to prevent from exiting application when presses home button. http://stackoverflow.com/questions/2079691/overriding-the-home-button-how-do-i-get-rid-of-the-choice – HelmiB Sep 24 '12 at 03:08
-
2you can refer this link http://nisha113a5.blogspot.in/2012/01/intercept-home-key-android.html to intercept home button key. – Hiren Dabhi Sep 24 '12 at 05:19
-
I'm doing kind of a lockscreen right after the mobile boots.therefore i do think there is a way to override home button. but thanks anyway :) – Marco Espinal Sep 26 '12 at 05:51
2 Answers
3
you can use:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
// don't write anything here to make back button not work
return true;
}
return super.onKeyDown(keyCode, event);
}
or you can use the onBackPressed()
but older version doesn't support this feature.
@Override
public void onBackPressed() {
// don't write anything here to make back button not work
}

Anubian Noob
- 13,426
- 6
- 53
- 75

philip
- 1,292
- 3
- 24
- 44
2
Overriding these buttons is a dangerous/bad idea and for that reason the home button can't be truly overridden permanently without the user confirming it. See this discussion: Overriding the Home button - how do I get rid of the choice?
The back button is a bit easier, just override onBackPressed:
@Override
public void onBackPressed() {
//do nothing
}

Community
- 1
- 1

LuxuryMode
- 33,401
- 34
- 117
- 188