NavUtils.shouldUpRecreateTask fails on JellyBean

后端 未结 3 1379
我在风中等你
我在风中等你 2021-02-13 10:22

I have an application that issues notifications that when selected start an activity. According to the Android docs I can use NavUtils.shouldUpRecreateTask to check whether the

3条回答
  •  伪装坚强ぢ
    2021-02-13 10:43

    This is not correct! When you start from the notification you have to create the stack when building the notification as explained here: http://developer.android.com/guide/topics/ui/notifiers/notifications.html#NotificationResponse

    Therefore when creating the notification you'll have to do this:

    Intent resultIntent = new Intent(this, ResultActivity.class);
    // ResultActivity is the activity you'll land on, of course
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    // Adds the back stack
    stackBuilder.addParentStack(ResultActivity.class);
    // Adds the Intent to the top of the stack
    // make sure that in the manifest ResultActivity has parent specified!!!
    stackBuilder.addNextIntent(resultIntent);
    // Gets a PendingIntent containing the entire back stack
    PendingIntent resultPendingIntent =
            stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
    

    and then when you click on the UP button you need the regular code, which is:

    if (NavUtils.shouldUpRecreateTask(this, intent)) {
        // This activity is NOT part of this app's task, so
        // create a new task when navigating up, with a
        // synthesized back stack.
        TaskStackBuilder.create(this)
        // Add all of this activity's parents to the back stack
                .addNextIntentWithParentStack(intent)
                // Navigate up to the closest parent
                .startActivities();
    } else {
        NavUtils.navigateUpTo(this, intent);
    }
    

    This works perfectly for me.

提交回复
热议问题