Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android通知打开最后一个活动,而不是正确的活动_Java_Android_Android Intent_Notifications_Android Pendingintent - Fatal编程技术网

Java Android通知打开最后一个活动,而不是正确的活动

Java Android通知打开最后一个活动,而不是正确的活动,java,android,android-intent,notifications,android-pendingintent,Java,Android,Android Intent,Notifications,Android Pendingintent,我有点问题,在很多地方我都找不到答案 我有一个服务,该服务检查站点上的通知,并提醒用户新的通知。当用户单击此通知时,它会将他们带到一个活动(我称之为CommentActivity) 但是,如果我点击应用程序内部的通知,什么也不会发生。如果我单击应用程序外部的通知,它将打开应用程序关闭前显示的最后一个活动 以下是显示通知的代码: Notice n; //Notification from site .... Intent nIntent = new

我有点问题,在很多地方我都找不到答案

我有一个服务,该服务检查站点上的通知,并提醒用户新的通知。当用户单击此通知时,它会将他们带到一个活动(我称之为CommentActivity)

但是,如果我点击应用程序内部的通知,什么也不会发生。如果我单击应用程序外部的通知,它将打开应用程序关闭前显示的最后一个活动

以下是显示通知的代码:

        Notice n; //Notification from site

        ....
        Intent nIntent = new Intent(Intent.ACTION_MAIN);
        nIntent.setClass(getApplicationContext(), CommentActivity.class);
        nIntent.putExtra("activity_id", n.getFeedID());
        nIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        PendingIntent pIntent = PendingIntent.getActivity(getApplicationContext(), 0, nIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);

        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(FlankNotificationService.this)
        .setSmallIcon(R.drawable.ic_launcher)
        .setContentTitle(n.getTitle())
        .setContentText(n.getText())
        .setContentInfo(arg0.getNotificationAmount() + " notification" + (arg0.getNotificationAmount() == 1 ? "." : "s."))
        .setContentIntent(pIntent);

        Notification mm = mBuilder.getNotification();

        mm.flags = Notification.FLAG_ONLY_ALERT_ONCE | Notification.FLAG_AUTO_CANCEL;
        mm.tickerText = n.getText();

        ....

        mNM.notify(1558, mm);
因此,它不是打开CommentActivity,而是恢复应用程序(如果应用程序未打开)或不执行任何操作(如果应用程序已打开)

多谢各位

编辑1:

将Intent.FLAG\u活动\u重新排序\u添加到Intent的前标志。仍然无法解决问题

编辑2:


我找到了问题的原因(这是一个相当愚蠢的原因)。当CommentActivity启动时,如果它没有从savedInstance或Intent.getExtra中获得任何数据,那么它将关闭。起初我没有想到这一点的原因是因为它甚至没有显示过渡。修复Intent.getExtra问题后,它现在可以工作了。感谢您提供的所有答案。

您可能需要将该标志添加到您的意图中。这将确保在处理意图时,
CommentActivity
是恢复的活动。

这就是它应该做的。这就像再次单击launcher中的应用程序图标一样。如果希望此活动始终显示,请在清单中使用android:clearTaskOnLaunch=“true”标记您的活动。是否将其添加到CommentActivity?我试过了,但仍然没有解决问题。+1这是添加标志后的最佳解决方案,仍然没有解决问题。我已经用添加了标志的新代码更新了问题。