单击通知图标时的 android 通知
我这样写了我的通知意图(下面的代码片段)。我正在运行活动 A、B 和 C。运行C时,我按下主屏幕,很快就收到了一条通知。我按下通知图标,希望从 Activity A 重新启动应用程序,但不幸的是它没有。这段代码当前的作用是在堆栈顶部创建一个新的活动 A...所以我陷入了以下正在运行的活动(或堆栈):A,B,C,A
所以我的主要问题是,我怎样才能清理 Activity 堆栈,以便单击通知图标时只有 Activity A 位于堆栈中?
任何提示或意见将不胜感激。
Intent notificationIntent = new Intent(context,
A.class);
notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
notificationIntent, 0);
notification.setLatestEventInfo(appContext, contentTitle,
contentText, contentIntent);
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) context
.getSystemService(ns);
mNotificationManager.notify(1, notification);
I wrote my notification intent like this(the snippet below). I have Activity A,B and C running. While running C, I pressed the home screen and soon received a notification. I pressed on the notification icon, hoping restart the app from Activity A but unfortunately it doesn't. What this code current do is create a new Activity A on top of the stack.... So I am stuck with the following running activity(or stack):A,B,C,A
So my main question is, how can I clean up the Activity stack so that only Activity A is on the stack when the notification icon is clicked?
Any tips or comments would appreciated.
Intent notificationIntent = new Intent(context,
A.class);
notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
notificationIntent, 0);
notification.setLatestEventInfo(appContext, contentTitle,
contentText, contentIntent);
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) context
.getSystemService(ns);
mNotificationManager.notify(1, notification);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在清单中为活动 A 设置 launchMode="singleTask"。在这种情况下,活动 A 将是您从通知返回后唯一正在运行的活动。
但是,当启动模式为 singleTask 或 singleInstance 时,当您从后台恢复应用程序时,您的应用程序将以相同的方式运行。无法恢复活动 B 或 C。
Set launchMode="singleTask" for the activity A in the manifest. In this case activity A will be the only one running after you go back to it from notification.
However when launch mode is singleTask or singleInstance your app is going to behave the same way when you resume your app from the background. It's not going to be possible to resume to activity B or C.
这个问题可能会帮助您FLAG_ACTIVITY_CLEAR_TOP 或使用
startActivityForResult
。This question may help you either with FLAG_ACTIVITY_CLEAR_TOP or using
startActivityForResult
.