gpt4 book ai didi

android - TaskStackBuilder.addParentStack() 和 TaskStackBuilder.addNextIntent() 有什么不同?

转载 作者:行者123 更新时间:2023-11-29 01:06:44 31 4
gpt4 key购买 nike

我尝试按照 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

我发现有人说我在使用addParentStack()时需要在AndroidManifest.xml中添加android:parentActivityName,我做了,但结果是一样的,什么也没发生。

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

  • addParentStack() 有什么作用?
  • addParentStack()addNextIntent() 有什么区别?
  • 我们需要使用 addParentStack() 吗?

提前致谢。

最佳答案

所以,我在官方文档上找到了这个

1.addParentStack()

它添加了后退堆栈(意味着如果你按下后退按钮,它将带你到你将给这个方法的 Activity )

2.addParentStack()和addNextIntent()的区别

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

3.我们需要addParentStack()吗?

如果您不想在按下后退按钮后转到父 Activity 或者您已在 list 文件中声明,则不是

以下代码来自官方文档,有助于理解

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());

关于android - TaskStackBuilder.addParentStack() 和 TaskStackBuilder.addNextIntent() 有什么不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46466870/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com