0

I have a main activity which is marked as singleInstance. I then have a notification which opens a different activity, below are both declarations:

<activity android:name="MainActivity" android:launchMode="singleInstance".....

<activity android:name="some.long.package.from.a.library.NotificationActivity" android:noHistory="true" android:launchMode="singleInstance" android:parentActivityName=".MainActivity">
  <meta-data android:name="android.support.PARENT_ACTIVITY" android:value=".MainActivity"/>
</activity>

When I create the notification I modify the task stack so it inserts the parent activity:

 Intent intent = new Intent(context, NotificationActivity.class);
 PendingIntent pendingIntent = TaskStackBuilder.create(context)
                .addNextIntentWithParentStack(intent)
                .getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

This all works great except for one little issue, when I touch the notification and go to the NotificationActivity and then use the back button or the back on the action bar, it goes to a whole new instance of MainActivity.

I know singleInstance isn't the ideal launch mode to use in most situations, however I have an application which is sort of like a web browser and ideally the user should get back exactly to where in the browser they were. I could save the page address and reload it (which I will for worst case scenario) but imagine if you were in the middle of filling out a long form and your browser did that just because you acknowledged a notification.

So can anyone tell me why singleInstance is being ignored after I modify the task stack on a pending intent?

Thanks.

casolorz
  • 8,486
  • 19
  • 93
  • 200

1 Answers1

1

You are trying to build a task stack that is logically inconsistent.

You've declared that MainActity has launchMode="singleInstance". This means that MainActivity must be the root activity in its task and that no other activities will be launched into this task (ie: the task containing MainActivity will have one and only 1 activity).

Then you try to build a task stack that contains MainActivity as the parent activity of NotificationActivity. This is inconsistent, so Android won't do it.

David Wasser
  • 93,459
  • 16
  • 209
  • 274