Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I detect user pressing HOME key in my activity? [duplicate]

Tags:

android

Can you please tell me how can my activity detect user pressing HOME key?

Thank you.

like image 662
n179911 Avatar asked Feb 05 '10 17:02

n179911


1 Answers

Update [August 2015]: As per the comment of JM Lord, this answer might not work as desired for devices Lollipop and above. See Alternative to getRunningTasks in Android L


Although, its an old question but most of the answers didn't work for me in API level 17 and above. I am answering what finally worked for me. I've tried many method but none of them are working as today. I tried answers on

Call method when home button pressed on android,

Detect home button press in android and

Overriding the Home button - how do I get rid of the choice?. The gist was that Home is like an emergency escape button. So you can't override it but can detect it in some ways.

Some of the ones I tried( including above answers) and didn't work are:

  1. Using keyCode==KeyEvent.KEYCODE_HOME in many ways as stated above. Now, if you read the documentation of KeyEvent.KEYCODE_HOME, it says that This key is handled by the framework and is never delivered to applications. So its no more valid now.

  2. I tried using onUserLeaveHint(). The documentation says:

    Called as part of the activity lifecycle when an activity is about to go into the background as the result of user choice. For example, when the user presses the Home key, onUserLeaveHint() will be called, but when an incoming phone call causes the in-call Activity to be automatically brought to the foreground.

    The issue with that is that the method also gets called when you start an Activity from within the activity where you are calling onUserleaveLint(), as was my case. See Android onBackPressed/onUserLeaveHint question for more. So its not sure that it would be call ONLY by pressing home button.

  3. Calling onStop(). It can also be called when an activity comes on top of the existing activity and fully covers it. So this also wont work.

Finally the following worked for me :

Seeing How to check current running applications in Android? you can say that if yours is the recent task that is shown on long pressing the home button, then it was send to background.

So, In your onPause() of the activity where you are trying to detect the home button pressed, you can check whether the application has been sent to background.

@Override
public void onPause() {
    if (isApplicationSentToBackground(this)){
        // Do what you want to do on detecting Home Key being Pressed 
    }
    super.onPause();
}

Function to check whether yours is the app which has been most recently sent to the background:

public boolean isApplicationSentToBackground(final Context context) {
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningTaskInfo> tasks = am.getRunningTasks(1);
    if (!tasks.isEmpty()) {
        ComponentName topActivity = tasks.get(0).topActivity;
        if (!topActivity.getPackageName().equals(context.getPackageName())) {
            return true;
        }
    }
    return false;
}

Thanks to @idish for pointing out that don't forget to include the following permission in your manifest:

<uses-permission android:name="android.permission.GET_TASKS" />

I am not sure whether there are any downsides of this, but it worked for me well. Hope it helps someone someday.

P.S: If you use this method in an Activity which has been launched with FLAG_ACTIVITY_NO_HISTORY flag, then this won't be useful as it checks the recent history for figuring out if Home button was clicked.

like image 185
Shobhit Puri Avatar answered Sep 30 '22 18:09

Shobhit Puri