Friday, January 25, 2013

Android: Solution to catch that the Home button was pressed

Home Button is like an emergency escape button.
Pressing Home twice will always take a user back to the center panel of the Home Screen, so whatever application they're running, it's easy for a user to get back to somewhere they know. You shouldn't really be overriding this unless you're producing a full Home replacement.

Don't  permanently override the Home button without the user confirming it.

We need to do follow three steps here...
1. Override onKeyDown and compare keycode with KeyEvent.KEYCODE_HOME
2. Override onAttachedToWindow()
3. add category to the activity because
CATEGORY_HOME : This is the home activity, that is the first activity that is displayed when the device boots.
<category android:name="android.intent.category.HOME"/>

Solution:  Source Code Here

2 comments :

  1. This solution is for lower version .Do we have the procedure for versions above ICS?

    ReplyDelete
    Replies
    1. There is no direct approach as we have for below ICS but If u want to trigger home button still we can do with flags.
      But by using flags the hardware keys are blocked and we should handle all the hardware keys function programmatically.

      Delete