通知点击:活动已打开 [英] Notification click: activity already open

查看:25
本文介绍了通知点击:活动已打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有通知的应用程序,如果我点击它们就会打开某个活动.我想要那个,如果我点击通知并且活动已经打开,它不会再次开始,而是被带到前面.

I have an application with notifications that open a certain activity if I click them. I want that, if I click the notification and the activity is already opened, it's not started again, but just brought to front.

我以为我可以使用标志 FLAG_ACTIVITY_BROUGHT_TO_FRONTFLAG_ACTIVITY_REORDER_TO_FRONT 来做到这一点,但它不断再次打开它,所以我有两次活动.

I thought I could do it with the flag FLAG_ACTIVITY_BROUGHT_TO_FRONT or FLAG_ACTIVITY_REORDER_TO_FRONT, but it keeps opening it again so I have the activity twice.

这是我的代码:

event_notification = new Notification(R.drawable.icon,
            mContext.getString(R.string.event_notif_message), System.currentTimeMillis()); 
Intent notificationIntent = new Intent(mContext, EventListActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
sendNotification(event_notification, notificationIntent, mContext.getString(R.string.event_notif_title),
                body, Utils.PA_NOTIFICATIONS_ID);

我可以用标志来管理它还是应该在 SharedPreferences 中存储一个变量来检查它是否打开?

Can I manage it with flags or should I store a variable in SharedPreferences to check if it's opened or not?

谢谢!

推荐答案

您需要将您正在启动的 ActivitylaunchMode 属性设置为 singleTop.当 Activity 已经位于任务堆栈的顶部时,这将导致传入的 Intent 被传递到现有实例,而不是启动一个新实例.

You need to set the launchMode attribute of the Activity you are starting to singleTop. This will cause incoming Intents to be delivered to the existing instance rather than starting a new instance when that Activity is already at the top of the task's stack.

这是在清单中通过将 android:launchMode="singleTop" 添加到 元素来完成的.要访问最新的 Intent(如果您对随它传入的任何数据感兴趣),请在您的 Activity 中覆盖 onNewIntent().

This is done in the manifest by adding android:launchMode="singleTop" to the <activity> element. To access the latest Intent (if you are interested in any data that may have passed in with it), override onNewIntent() in your Activity.

这篇关于通知点击:活动已打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆