TaskStackBuilder and extras for the back stack

后端 未结 3 1750
失恋的感觉
失恋的感觉 2020-12-13 09:17

I\'m trying to use TaskStackBuilder with notifications to create a back stack for the back button to go through. Normal flow of my app:

  1. Activity A is launched
3条回答
  •  醉话见心
    2020-12-13 09:42

    I have struggled with this one. My research resulted in the following solution (very similar to @pawelzieba but with one important deletion):

    Intent activityA = new Intent(context, ActivityA.class);
    activityA.putExtra(key, valueA);
    Intent activityB = new Intent(context, ActivityB.class);
    activityB.putExtra(key, valueB);
    Intent activityC = new Intent(context, ActivityC.class);
    activityC.putExtra(key, valueC);
    
    TaskStackBuilder sBuilder = TaskStackBuilder.create(this)
        .addNextIntent(activityA);
        .addNextIntent(activityB);
        .addNextIntent(activityC);
    PendingIntent pIntent = sBuilder.getPendingIntent( 0, PendingIntent.FLAG_UPDATE_CURRENT );
    

    Note that you should not modify your manifest with the parent associations, and you do not want to use:

    sBuilder.addParentStack( ActivityC.class );
    

    or else you will get repeats of ActivityA and ActivityB in the newly created task that have intents without extras. If you do not need to pass intents with extras, you could add the parent associations in the manifest and call addParentStack().

提交回复
热议问题