2

我提出了一个通知。我想要的是,当用户点击通知时,我的活动被带到前面而不创建它的新实例。为此,我添加了标志 REORDER_TO_FRONT,但当我单击通知时,仍然调用 oncreate 而不是 onNewIntent。

这是我的代码 -

        int icon = R.drawable.android;
        long when = System.currentTimeMillis();
        CharSequence text = "new message";
        CharSequence contentTitle = stanza.getFrom();  // message title
        CharSequence contentText = stanza.getBody();      // message text

        Intent notificationIntent = new Intent(context, ChatBox.class);
        notificationIntent.putExtra("buddyid",stanza.getFrom());
        notificationIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0,notificationIntent, 0);


// the next two lines initialize the Notification, using the configurations above
        Notification notification = new Notification(icon, text, when);
        notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
        mNotificationManager.notify(1,notification);
4

2 回答 2

0

你试过吗:

Intent.FLAG_ACTIVITY_CLEAR_TOP

与您的 notificationIntent.addFlag();

于 2012-09-21T11:52:55.440 回答
0

我的解决方案是制作一个广播接收器来监听通知触发的广播动作。所以基本上:

  1. 通知会触发一个广播操作,其中包含要启动的活动的额外名称。

  2. 广播接收器在单击通知时捕获此信息,然后使用 FLAG_ACTIVITY_REORDER_TO_FRONT 标志创建启动该活动的意图

  3. 活动被带到活动堆栈的顶部,没有重复。

于 2015-06-12T20:07:19.037 回答