Search code examples
androidandroid-homebuttonandroid-background

Detect HomeButton pressed Android before onBackPressed


I'm developing an android application in which when the app is in background it displays on the status bar a notification. Using onUserLeaveHint I can detect when the user presses the HOME button, but the same event listener also triggered when the user presses the BACK Button. How can I detect HOME button presses only?


Solution

  • I'm sorry to say but all the suggested answers are obsolete and some of them are downright wrong.

    Intercepting HOME directly was blocked way back on Froyo due to security issues and a fear of malware (if you can intercept HOME you can try and hijack the device).

    The only solution i'm aware of which seperates BACK from HOME is to to intercept the onNewIntent() event listener.

    onNewIntent() is fired when an app is running, and receives another intent to be launched. That is why you will get it when HOME is pressed.

    When BACK is pressed, your app is not going to receive an intent. All that happens is that the apps on top of yours are removed. So your app appears from the back stack with only onResume() being called.

    So that is how you can tell.

    It is also mentioned here. Goodluck.