TaskStackBuilder.addParentStack()和TaskStackBuilder.addNextIntent()之间有什么不同?

时间:2017-09-28 10:28:53

标签: android notifications back-stack android-8.0-oreo

我尝试按照Android教程测试Android O中的Notification Channel。当我进入显示通知的步骤时,我看到他们使用TaskStackBuilder,我想知道addParentStack()在做什么? 因为当我删除这行代码时它仍然可以正常工作。

我尝试通过添加更多这样的父级来测试它:

private PendingIntent getPendingIntent() {
    Intent openMainIntent = new Intent(this, MainActivity.class);
    Intent openMain2Intent = new Intent(this, Main2Activity.class);
    Intent openMain3Intent = new Intent(this, Main3Activity.class);

    // The stack builder object will contain an artificial back stack for the
    // started Activity.
    // This ensures that navigating backward from the Activity leads out of
    // your application to the Home screen.
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    // Adds the back stack for the Intent (but not the Intent itself)
    stackBuilder.addParentStack(MainActivity.class);
    stackBuilder.addParentStack(Main2Activity.class);

    // Adds the Intent that starts the Activity to the top of the stack
    stackBuilder.addNextIntent(openMain3Intent);
    return stackBuilder.getPendingIntent(0, PendingIntent.FLAG_ONE_SHOT);
}

如上面的代码,我添加了多个父级,并期望当我从Main3Activity按回来时,它应分别转到Main2ActivityMainActivity,但结果是它返回到主页屏幕立即。

我通过移除addParentStack()并添加了更多addNextIntent()来更改它:

private PendingIntent getPendingIntent() {
    Intent openMainIntent = new Intent(this, MainActivity.class);
    Intent openMain2Intent = new Intent(this, Main2Activity.class);
    Intent openMain3Intent = new Intent(this, Main3Activity.class);

    // The stack builder object will contain an artificial back stack for the
    // started Activity.
    // This ensures that navigating backward from the Activity leads out of
    // your application to the Home screen.
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);

    // Adds the Intent that starts the Activity to the top of the stack
    stackBuilder.addNextIntent(openMainIntent);
    stackBuilder.addNextIntent(openMain2Intent);
    stackBuilder.addNextIntent(openMain3Intent);
    return stackBuilder.getPendingIntent(0, PendingIntent.FLAG_ONE_SHOT);
}

按照我的预期,一切正常,当我按下按钮时,它打开Main3Activity并转到Main2ActivityMainActivity

我发现有些人说当我使用android:parentActivityName时,我需要在AndroidManifest.xml中添加addParentStack(),我做了但结果是一样的,没有任何反应。

所以,如果有人能告诉我,我将不胜感激:

  • addParentStack()做什么?
  • addParentStack()addNextIntent()之间的区别是什么?
  • 我们需要使用addParentStack()吗?

提前感谢。

1 个答案:

答案 0 :(得分:1)

所以,我在官方文件中找到了这个

<强> 1.addParentStack()

它会添加后退堆栈(意味着如果您按下后退按钮,它将带您进入此方法的活动)

2. addParentStack()和addNextIntent()之间的距离

基本区别是addnextIntent()会将意图添加到堆栈顶部

3.我们需要addParentStack()吗?

如果您不想在按下后退按钮后转到父活动,或者您已在清单文件中声明

以下代码来自官方文档,它将帮助您理解

int id = 1;
...
Intent resultIntent = new Intent(this, ResultActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack
stackBuilder.addParentStack(ResultActivity.class);
// Adds the Intent to the top of the stack
stackBuilder.addNextIntent(resultIntent);
 // Gets a PendingIntent containing the entire back stack
 PendingIntent resultPendingIntent =
    stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
 ...
 NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
 builder.setContentIntent(resultPendingIntent);
 NotificationManager mNotificationManager =
 (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
 mNotificationManager.notify(id, builder.build());