【问题标题】:Android Notification Opens On Existing ActivityAndroid 通知在现有活动上打开
【发布时间】:2014-08-28 23:16:05
【问题描述】:

我在通知意图方面遇到了麻烦。我有一个创建通知的服务(服务检查消息并创建通知)。应用程序有一个操作栏,用户可以通过滑动菜单在活动之间导航。

http://i.stack.imgur.com/YJXe6.png

当用户点击通知时,它会在当前活动上打开一个新活动。(就像一个独立的新实例)。我想在与用户手动导航相同的实例中打开它们(就像在 B 活动上单击 A ie)

我当前的活动启动模式是标准的(虽然我尝试了 singleTop 和 singleTask 和 flags)

当前通知代码:

Intent i = null;
i = new Intent(this, MessagesListActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,i, 0);
Builder notificationBuilder  = new NotificationCompat.Builder(this);
notificationBuilder.setContentTitle(title);
notificationBuilder.setContentText(msg);
notificationBuilder.setSmallIcon(R.drawable.speech_bubble_orange);
notificationBuilder.setContentIntent(contentIntent);
notificationBuilder.setAutoCancel(true);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = notificationBuilder.build();
notificationManager.notify(Constants.UNREADMESSAGESNOTIFICATIONID,notification); 

感谢您的帮助。

【问题讨论】:

    标签: java android android-activity notifications


    【解决方案1】:

    我解决了这个问题。使用这种组合,新启动的活动可以清除堆栈。我也没有更改启动模式(仍然是标准)

    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    

    【讨论】:

      【解决方案2】:

      在您的清单中,您必须将您的活动标记为单个实例,并保留单个任务

      android:launchMode= "singleTask" | "singleInstance"
      

      此外,您可能必须从您的意图中删除单个顶部标志。

      【讨论】:

      • 试过了,不工作它仍然会从当前应用程序中单独启动活动。顺便说一句,在活动之间导航时,我会杀死以前的活动,因为它们也在默认情况下在当前活动上打开(更改为 singleTask/instance 不会改变这种行为)。我正在考虑一种解决方法,例如在单击通知时终止以前的活动。还是谢谢
      【解决方案3】:

      如果我理解正确,我认为您正在寻找的是TaskStackBuilder。它允许您创建一个后台堆栈,以提供正确导航到由PendingIntent 启动的Activity

      有关更多信息,请参阅文档here

      【讨论】:

        猜你喜欢
        • 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
        相关资源
        最近更新 更多