如何在单击通知后关闭上一个 activity 并打开一个新的 activity
How to close previous activity after the notification is clicked and opens a new activity
我正在尝试使用 android 中的通知,我想要发生的是,每当我单击通知时,它都会打开新的 activity,并会自动关闭activity 我之前上过。我尝试添加一些标志,但它没有用,它不断地在另一个之上堆积 activity。任何关于如何使这项工作的建议将不胜感激。
这是我的代码片段:
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());
}
使用这个:
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
|Intent.FLAG_ACTIVITY_CLEAR_TASK);
看看有没有帮助
我正在尝试使用 android 中的通知,我想要发生的是,每当我单击通知时,它都会打开新的 activity,并会自动关闭activity 我之前上过。我尝试添加一些标志,但它没有用,它不断地在另一个之上堆积 activity。任何关于如何使这项工作的建议将不胜感激。
这是我的代码片段:
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());
}
使用这个:
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
|Intent.FLAG_ACTIVITY_CLEAR_TASK);
看看有没有帮助