Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call method when home button pressed

I have this method in one of my Android Activities:

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

But, even though the KEYCODE_HOME is valid, the log method never fires. This works for the back button though. Does anyone know why this is and how to get this to work?

like image 598
ingh.am Avatar asked Jan 24 '11 15:01

ingh.am


People also ask

Which method gets called when Home button pressed?

Pressing the Home key allows the user to start a new Task, by showing the launcher. All active Tasks (and therefore Activities incl. your "Air Control" example) will call their onPause() method.

Which method is called when a button is pressed?

This answer is useful. 0. This answer is not useful. Show activity on this post. The activities onPause() is called when pressing the home or back button.

Which lifecycle method will be called when the home button is pressed when you are interacting with the activity?

onPause() is called when Home button is pressed.

Which of the following methods is are not called when home button is pressed?

Note: onDestroy() method not call after press Home Button.


1 Answers

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.

Take a look at this discussion.

You will notice that the home button seems to be implemented as a intent invocation, so you'll end up having to add an intent category to your activity. Then, any time the user hits home, your app will show up as an option. You should consider what it is you are looking to accomplish with the home button. If its not to replace the default home screen of the device, I would be wary of overloading the HOME button, but it is possible (per discussion in above thread.)

like image 101
Nick Campion Avatar answered Oct 10 '22 07:10

Nick Campion