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?

- 769
- 5
- 18

- 1,633
- 3
- 24
- 38
-
have you tried overriding onKeyDown and checking for KeyEvent.KEYCODE_HOME ? – Breadbin Apr 16 '15 at 11:06
-
@Breadbin `KeyEvent.KEYCODE_HOME` is not intercepted in `onKeyDown` since the times of Froyo and Gingerbread. – Royi Benyossef Apr 16 '15 at 13:50
-
Use **onTrimMemory()** as suggest from [here](https://stackoverflow.com/a/55129289/14446786) – scr Dec 10 '20 at 08:03
4 Answers
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.

- 1
- 1

- 769
- 5
- 18
When the user presses back, you should receive a call to onBackPresssed
. You could use this to set a flag so that you can determine during onUserLeaveHint
if the back button was pressed. Remember to clear the flag afterwards.

- 1,014
- 9
- 10
-
I try this solution. But sometimes it calls onUserLeaveHint before onBackPressed so I can't set the flag. – Manuel Castro Apr 16 '15 at 11:09
I found a solution using the onStop()
and onResume()
method.
That's my code
@Override
public void onBackPressed() {
Intent intent = new Intent(this, PagerActivity.class);
startActivity(intent);
finish();
}
/* Handle notification create/destroy */
private Boolean notificationCreated = false;
@Override
protected void onStop() {
Utils.createNotification();
notificationCreated = true;
super.onPause();
}
@Override
protected void onResume() {
super.onResume();
if (notificationCreated)
{
Utils.cancelNotification();
notificationCreated = false;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (notificationCreated) {
Utils.cancelNotification();
notificationCreated = false;
}
}

- 1,633
- 3
- 24
- 38
You can just simply deactivate the back button for that specific page. Here is what works for me:
@Override
public void onBackPressed() {
}

- 392
- 5
- 7
-
Overriding the `onBackPressed` is against the Android app checklist (http://developer.android.com/distribute/essentials/quality/core.html) App does not redefine the expected function of a system icon (such as the Back button). – Royi Benyossef Apr 16 '15 at 13:54