单击通知并打开新活动后如何关闭以前的活动

new*_*829 2 android android-notifications

我正在尝试使用 android 中的通知,我想要发生的是,每当我单击通知时,它都会打开新的活动,并会自动关闭我之前的活动。我尝试添加一些标志,但没有用,它继续将活动堆积在另一个之上。任何关于如何进行这项工作的建议将不胜感激。

这是我的代码片段:

protected void displayNotification() {
        Intent i = new Intent(context, Order.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

        notification = new NotificationCompat.Builder(context);
        notification.setContentTitle("Received New Order/s");
        notification.setContentText("Refresh Order List");
        notification.setTicker("Receive New Order/s");
        notification.setDefaults(Notification.DEFAULT_VIBRATE | Notification.DEFAULT_SOUND);
        notification.setWhen(System.currentTimeMillis());
        notification.setSmallIcon(R.drawable.my_icon);
        notification.setContentIntent(PendingIntent.getActivity(context, 0, i, PendingIntent.FLAG_UPDATE_CURRENT));
        notification.setAutoCancel(true);

        NotificationManager nm = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
        nm.notify(uniqueID,notification.build());

    }
Run Code Online (Sandbox Code Playgroud)

Abh*_*wal 5

用这个:

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
|Intent.FLAG_ACTIVITY_CLEAR_TASK);
Run Code Online (Sandbox Code Playgroud)

看看它是否有帮助