【问题标题】:TaskStackBuilder in PendingIntent not workingPendingIntent 中的 TaskStackBuilder 不起作用
【发布时间】:2015-07-18 13:01:49
【问题描述】:

我正在尝试使用此代码按下通知时为 Activity 重新创建回栈:

Intent firstIntent = new Intent(this, First.class);  
Intent secondIntent = new Intent(this, Second.class);    
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);

 stackBuilder.addNextIntent(firstIntent);
 stackBuilder.addNextIntent(secondIntent);
 PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); 
 Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

 NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                                    .setSmallIcon(R.mipmap.ic_launcher)
                                    .setContentTitle(fromName)
                                    .setContentText(message)
                                    .setAutoCancel(true)
                                    .setSound(defaultSoundUri)
                                    .setContentIntent(pendingIntent);

  NotificationManager notificationManager =
                                    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

  notificationManager.notify(0 ,notificationBuilder.build());

但它不起作用。当我按下通知时,第二个活动已打开但按下后退按钮,活动已完成,而我想返回第一个活动。

在 AndroidManifest 中设置 parentActivityName 不起作用。

怎么了?

谢谢

【问题讨论】:

  • 你的 build.gradle 中的 minSdkVersion 是什么?

标签: android android-activity android-notifications back-stack


【解决方案1】:

尝试改变

PendingIntent.FLAG_UPDATE_CURRENT

PendingIntent.FLAG_CANCEL_CURRENT

【讨论】:

  • 谢谢!这对我有用。你能解释一下为什么会这样吗?
  • @SaketJain:说实话不知道。我遇到了同样的问题,遇到了一些 SO 答案(现在找不到),它建议使用 FLAG_CANCEL_CURRENT 标志而不是 FLAG_UPDATE_CURRENT ,结果证明对我来说效果很好。这更像是一种解决方法,而不是适当的解决方案,因为在some Samsung devices using Lollipop FLAG_CANCEL_CURRENT flag throws crash 上。如果我能通过适当的解释找到解决这个问题的好方法,我会更新我的答案。
  • 谢谢。我目前已经使用 CANCEL 标志实现了它,但是,我实际上也更喜欢更好的方法。让我们继续寻找:)
  • 我试过这个,但实际上它对我在 android API 22 中的应用没有任何影响,也许它对其他人有影响。我强烈建议在这方面咨询官方文档,因为它有助于解决同样的问题
  • @GaneshMohan 谢谢它的工作。只是想知道这是正确的方法吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多