androidandroid-activitynotificationsandroid-pendingintenttaskstackbuilder

Using TaskStackBuilder in notification recreates an activity


I have a notification that is shown to the user when some event occur in the activity. When I create the notification, I use TaskStackBuilder to ensure that I have backstack activity list if my activities are cleared up by the system. My code it looks like this:

    ...
    TaskStackBuilder builder = TaskStackBuilder.create(sContext);
    builder.addNextIntent(new Intent(context, ActivityA));
    builder.addNextIntent(new Intent(context, ActivityB));
    builder.addNextIntent(new Intent(context, ActivityC));
    PendingIntent pendingIntent = 
            builder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT)

    Notification.Builder builder = new Notification.Builder(context)
            .setContentTitle(title))
            .setContentText(notificationText)
            .setSmallIcon(getNotificationIcon())
            .setContentIntent(pendingIntent);

    builder.build();
    ...

So, the opened activity will be the last added and that's ActivityC (and I don't want it to be recreated). The one solution I have found is this: How to make notification resume and not recreate activity? It is stated that I shouldn't use TaskStackBuilder, but is not what I want. I want to start an activity from notification, using TaskStackBuilder and not recreating the whole activity.

Any solutions/suggestions?


Solution

  • If you just want to use the notification to allow the user to return to the task in whatever state it was in, then you don't need TaskStackBuilder for that.

    See: