Reputation: 755
I'm getting an unexpected behavior:
When I have closed the app and open it from the notification, the Activity B is started.
So, when I click on HOME button, the app navigates to the Activity A. Then, I push the BACK button and the app goes to background and I get the Android main screen.
This is OK, but if I open the recent apps menu and select again my app, it loads the Activity B instead Activity A as I expect. I don't really know what is happening.
The intent set in the notification builder is:
Intent intent = new Intent(this, ActivityB.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
In Activity B I have:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (id == android.R.id.home) {
Intent intent = new Intent(ActivityB.this, ActivityA.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
Any idea or suggestion?
Upvotes: 1
Views: 1032
Reputation: 755
I found a solution which fit in what I'm expecting. When I receive a notification I create a new Task Stack with my mainActivity on the top:
Intent intent = new Intent(this, ActivityB.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(ActivityB.class);
stackBuilder.addNextIntent(intent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
Now, I can also remove the overrided check for HOME button in the Activity B:
// This can be now removed
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (id == android.R.id.home) {
Intent intent = new Intent(ActivityB.this, ActivityA.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
Upvotes: 1
Reputation: 95578
The reason this happens is that the task which is in the list of recent tasks was started from the Notification
with ActivityB
as the root Activity
. If you choose this task from the list of recent tasks, Android will bring the existing task to the foreground. However, if the task has no active activities in it (all the activities have been finished), then Android starts the root Activity
from that task again.
You need to prevent the app from being added to the list of recent tasks if it is started from a Notification
. You can do that by adding:
android:excludeFromRecents="true"
to the manifest entry for ActivityB
.
Upvotes: 1
Reputation: 4643
You can use FLAG_ACTIVITY_REORDER_TO_FRONT
flag instead, which will only reorder the activities, instead of FLAG_ACTIVITY_CLEAR_TOP
which will remove other activities on top of it.
For example, when using FLAG_ACTIVITY_REORDER_TO_FRONT
consider a task consisting of four activities: A, B, C, D
. If D
calls startActivity()
with an Intent
that resolves to the component of activity B, then B will be brought to the front of the history stack, with this resulting order: A, C, D, B
. This flag will be ignored if FLAG_ACTIVITY_CLEAR_TOP
is also specified.
you can refer to this link: https://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_REORDER_TO_FRONT
Upvotes: 0