2017-09-28 147 views
1

我试着按照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回应该分别去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当我按下后退按钮。

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

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

  • 什么是addParentStack()吗?
  • addParentStack()addNextIntent()有何不同?
  • 我们是否需要使用addParentStack()

提前致谢。

回答

1

所以,我发现这个在官方文件

1.addParentStack()

它增加了回堆栈(也就是说,如果你按后退按钮它会带你到你的活动将给予此方法)addParentStack()和addNextIntent()之间

2.Diff

基本区别是addnextInte nt()将意图添加到栈顶

3.我们需要addParentStack()吗?

不是如果你不想去父活动按后退按钮后,或者你已经在manifest文件中声明

下面的代码是从官方文件,它会帮助你理解这个

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

谢谢回答。你看到我第一个例子的结果了吗?我期望它回到我添加的父母身上,但不是。或者我想念一些东西? –

+0

我还没有尝试过你的代码,我会尝试一下,然后会告诉你 –

+0

使用addParentStack()多于一次将无法工作,因为我们只能有一个父活动,并且你的问题可以用addNextIntent()来解决,因为在在这种情况下,您正在向后台添加意图,以便当您按下后退按钮时,它将带您前往位于后台的下一个目的地 –

相关问题