我试着按照android教程在Android O中测试Notification Channel。当我进入显示通知的步骤时,我看到他们使用TaskStackBuilder
,我想知道addParentStack()
在做什么? 因为当我删除这行代码它仍然工作正常。TaskStackBuilder.addParentStack()和TaskStackBuilder.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 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
回应该分别去Main2Activity
和MainActivity
但结果是立即返回到主屏幕。
我通过移除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
,去Main2Activity
和MainActivity
当我按下后退按钮。
我发现有人说我需要在AndroidManifest.xml
中加android:parentActivityName
当我用addParentStack()
时,我做了,但结果是一样的,什么也没有发生。
所以,我将不胜感激,如果有人可以告诉我:
- 什么是
addParentStack()
吗? addParentStack()
和addNextIntent()
有何不同?- 我们是否需要使用
addParentStack()
?
提前致谢。
谢谢回答。你看到我第一个例子的结果了吗?我期望它回到我添加的父母身上,但不是。或者我想念一些东西? –
我还没有尝试过你的代码,我会尝试一下,然后会告诉你 –
使用addParentStack()多于一次将无法工作,因为我们只能有一个父活动,并且你的问题可以用addNextIntent()来解决,因为在在这种情况下,您正在向后台添加意图,以便当您按下后退按钮时,它将带您前往位于后台的下一个目的地 –