5

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?

Community
  • 1
  • 1
NixSam
  • 615
  • 6
  • 20
  • Please explain in more detail exactly what you want to do. In general, using `TaskStackBuilder` will cause any existing task to be reset (ie: cleared) and the activities in the task to be recreated. – David Wasser Nov 05 '15 at 15:42
  • @DavidWasser I have a service in a background that do some work. It could last long, so user will leave the activity and could go back to it by pressing notification. Activity could be cleaned up by the system in meanwhile. That's why I'm using TaskStackBuilder. – NixSam Nov 06 '15 at 08:49
  • 1
    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 http://stackoverflow.com/questions/6575730/notification-to-restore-a-task-rather-than-a-specific-activity or http://stackoverflow.com/a/5502950/769265 or http://stackoverflow.com/questions/30544165/starting-app-only-if-its-not-currently-running/30576053#30576053 – David Wasser Nov 06 '15 at 16:28
  • @DavidWasser That looks like something that will work. Thanks for the answer. Maybe you could post is as an answer and remove -1, so that others with similar problem have a solution. Thanks again. – NixSam Nov 09 '15 at 10:45
  • I have added an answer. Hopefully this solves your problem. The -1 isn't from me so I can't remove it. – David Wasser Nov 09 '15 at 12:58
  • 1
    4 years later and I ran into this problem. The third answer suggested from @DavidWasser seems to be working out fine in my case (using `PackageManager.getLaunchIntentForPackage()`). – Carmen Oct 02 '19 at 11:58

2 Answers2

2

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:

Community
  • 1
  • 1
David Wasser
  • 93,459
  • 16
  • 209
  • 274
-1
TaskStackBuilder builder = TaskStackBuilder.create(sContext); 
Intent intentForA = new Intent(context, ActivityA);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)
builder.addNextIntent(intentForA); 

The flag will not recreate the activity if its already there and will just pass on the new Intent to the onNewIntent() method.For more details check out the documentation

http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP

jily
  • 344
  • 2
  • 11
  • 3
    This is so wrong. First of all, using `TaskStackBuilder` negates all of this because `TaskStackBuilder` forces a reset of the task at the beginning, regardless of whatever flags you provide. Secondly, in a non-`TaskStackBuilder` case, the `CLEAR_TOP` flag ONLY will prevent recreation of the `Activity` if the flag `SINGLE_TOP` is also used (or if the `Activity` has `launchMode="singleTop"` in the manifest declaration. – David Wasser Nov 05 '15 at 15:49
  • 1
    @DavidWasser you are right about the addition of SINGLE_TOP flag. – jily Nov 06 '15 at 08:24
  • @jily As David Wasser said, the whole task of our application will be replaced with new task if we use TaskStackBuilder. So, I don't think we could do anything to prevent recreation of activity. But thanks for helping... – NixSam Nov 06 '15 at 08:49