我尝试按照android教程在Android O中测试通知频道。当我进入显示通知的步骤时,我看到他们使用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
按下时应分别回到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
。我发现有人说我在使用
android:parentActivityName
时需要在AndroidManifest.xml
中添加addParentStack()
,我这样做了,但是结果是一样的,什么也没发生。因此,如果有人可以告诉我,我将不胜感激:
addParentStack()
是做什么的?addParentStack()
和addNextIntent()
有什么区别?我们需要使用
addParentStack()
吗?预先感谢。
最佳答案
所以,我在官方文件上发现了这个
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());
关于android - TaskStackBuilder.addParentStack()和TaskStackBuilder.addNextIntent()有什么区别?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46466870/