I override method onKeyDown in activity. But it is called only when back button is pressed. It is not called when home button is pressed. I am testing this at Nexus 5. What is the reason of that?
1 Answers
I'm afraid that in the conventional sense this cannot be done. Imagine the issues end-users could/would have if developers could override the home button? People could use it to stop users from exiting their app. Its a big no no!
However... If you're not up to anything sinister, then there are ways you can intercept an assumption that the home button has been pressed. For example:
When the home button is pressed, the OS will fire off a onStop
. So with that in mind you could override that method, like so:
@Override
protected void onStop()
{
super.onStop();
// Add your HOME button press logic here
}
Things to keep in mind when using anything that 'assumes' the Home button is pressed:
- When popping to another
Activity
, you'll receive this trigger. - This method is also triggered at various other points by the OS.
I guess it would come down to you figuring out why you need to intercept the home button press, and if there was any other way around it you could use.
Long story short:
No you can't officially intercept the Home button.
You can assume, but be faced with random triggers also calling the onStop()
Look for another option and ignore the home button completely. No one NEEDS to intercept it.
Hope this helps!

- 5,197
- 5
- 38
- 70
-
I want to Kill my app if another app is launched. How can I do that? – M. Usman Khan Oct 13 '17 at 01:49
-
Please ask a new question, rather than hijack an existing. But for reference, that goes against the app lifecycle. Your app will be paused while a new application takes focus. – laminatefish Oct 13 '17 at 09:16