In my application A is the main Activity. Its navigate something like this A, B, C, D etc. I implemented google cloud push notification . When push notification comes notification produces and click on it goes to activity X. When stack is not empty there is no problem . When stack is empty and activity x launched from the push notification act as launch activity .That means when again the stack is empty if we long press the home button and select the app navigate to X actvity( activity launched from notification) instead of A activity.Please help me.
1 Answers
The simplest thing that would be is to override onUserLeaveHint()
and finish your Activity,
From Docs about protected void onUserLeaveHint()
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, onUserLeaveHint() will not be called on the activity being interrupted. In cases when it is invoked, this method is called right before the activity's onPause() callback.
This callback and onUserInteraction() are intended to help activities manage status bar notifications intelligently; specifically, for helping activities determine the proper time to cancel a notfication.
Psuedo Code,
@Override
protected void onUserLeaveHint() {
super.onUserLeaveHint();
finish();
}
You can also check this answer
for further reference.
UPDATE:
Another work around to detect your HOME Key is pressed is check the Activity in ActivityManager. When HOME button is pressed your BaseActivity remains at second position in the Activity Lists. So you can check that using ActivityManager as below,
@Override
protected void onUserLeaveHint() {
super.onUserLeaveHint();
Log.e("onUserLeaveHint()", "Yes");
ActivityManager activityManager = (ActivityManager)
getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> recentTasks = activityManager
.getRunningTasks(Integer.MAX_VALUE);
for (int i=0; i<recentTasks.size(); i++) {
if (i == 1 && recentTasks.get(i).baseActivity.toShortString()
.indexOf(getPackageName()) > -1) {
Log.e("Home Pressed", "Yes");
break;
}
}
}
Also, don't forget to add
<uses-permission android:name="android.permission.GET_TASKS"/>
Permission in the AndroidManifest file. Further reference can be found from this blog
.

- 1
- 1

- 67,150
- 23
- 161
- 242
-
But I have to launch the main activity when stack is empty.But when stack is empty and launched activity from notification it acts as launch activity later. – dmp Aug 27 '12 at 11:09
-
yes you can detect when home button is pressed and finish the activity. – Lalit Poptani Aug 27 '12 at 11:12