Pre Exam Test Practice (SSC | Banking | IAS | GATE | State Level | CLAT | Railways | Insurance | Teaching Exams | NDA | Defence | IES |State Exam )

Organizational Research By

Surprising Reserch Topic

detect home button press in android

detect home button press in android  using -'android,android-homebutton'

This has been driving me nuts for a while now.

Is there any way of reliably detecting if the home button has been pressed in an android application?

Failing that, is there a robust way of telling what caused an activity to go into onPause? i.e Can we detect if it was caused by a new activity launching or by pressing back/home.

One suggestion I have seen is to override onPause() and call isFinishing() but this will return false when pressing the home button just as it would if a new activity is starting so this fails to distinguish between the two.

Any help much appreciated.

** Update** :
Thanks to @android-hungry for this link:

Overiding the following method:

public void onAttachedToWindow() {

Then the following event WILL get fired for home button presses:

public boolean onKeyDown(int keyCode, KeyEvent event) {     

    if(keyCode == KeyEvent.KEYCODE_HOME)
       //The Code Want to Perform.

I'm not sure if there are any side effects with this line:


So it would seem that contrary to popular belief, you can in fact listen out for the home key. Worryingly, you can return false and have the home key do nothing.

As expected, there are some side affects with this - it seems that embedded videos and google maps are not visible with this mode enabled.

Supposedly this hack no longer works as of Android 4.0 onwards

asked Sep 15, 2015 by GreO53
0 votes

Related Hot Questions

Government Jobs Opening