How To Override Back & Home Button In An Android App

To override the back button in an Android app, you just need to capture when the user presses the back button by overriding the onKeyDown() method and onBackPressed() methods. For the home button, the onUserLeaveHint() method gets always called.

These methods belong to the Activity class and must be overriden in order for your app to do what you intend it to do. One of the Android developers actually posted an article on the best way to ensure that your code will work across all versions of the Android platform whether old or new.

Check out the codes below.

Okay, so the above methods are for capturing when the user presses the back button. What about the home button? The answer is, there is no way. This is by design because the home button is the user’s only means of escape in case some malware app that you just opened threatens to harm your phone.

However, I did find a hack that can detect when the home button is pressed by checking the tasks of the ActivityManager. I noticed that whenever the home button is pressed, my app’s Activity is always second in the list so that is my basis in determining it. This could also apply if there is an incoming phone call although I have not tested this. Let me know if this works too.

Check out the code below.

It is worth noting that the index position of the Activity may be different with how your app starts an activity. You can check the output in the loop to see which index your Activity class falls under.

Related Posts Plugin for WordPress, Blogger...
  • Ak

    @Override
    protected void onUserLeaveHint() {
    super.onUserLeaveHint();
    exitAppAnimate();
    }

    private void exitAppAnimate() {
    ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    List recentTasks = activityManager.getRunningTasks(Integer.MAX_VALUE);
    for (int i=0; i -1) {
    // home button pressed
    break;
    }
    }
    }

    what is the condition of for (int i=0; i -1)

  • admin

    @ak: hi, i fixed the post. the code messed up because i switched to a different code plugin for wordpress. thank you

  • IM

    Hi!
    I’m working on my first android application which happens to also be my FYP. One of the things I need to do is to override the home button, so I tried what you suggested, but I got a SecurityException: Permission Denial: getTasks () from pid = 820, uid =10047 requires android.permission.GET_TASKS

    So, I added a uses permission to the manifest, but that just re-enabled the home button

    • admin

      @IM: i am not sure which android version you are using it with but if it is the latest, maybe things have changed and they really blocked the onUserLeaveHint() method from getting called.

      as mentioned in my post, there is no way you can override the home button because that is made by design.

      better leave it at that.

  • IM

    Yeah, it is the latest. I guess I have no choice but to leave it. Thank you!

  • http://careers.stackoverflow.com/richardlemesurier @mobiRic

    onUserLeaveHint() can not be relied upon to catch phone calls coming in. It is not called (at least not anymore) when a phone call comes in. In fact, from the phone call, you can press home, and navigate to any app, onUserLeaveHint() will still not be called.

    Just some info I’ve found while working on a solution similar to yours.

    Best luck to you