我有一个带有通知的应用程序,如果我点击它们就会打开某个活动 . 我想要的是,如果我点击通知并且活动已经打开,那么 not 再次启动,但只是被带到了前面 .
我以为我可以用旗帜 FLAG_ACTIVITY_BROUGHT_TO_FRONT
或 FLAG_ACTIVITY_REORDER_TO_FRONT
来做,但它会继续打开它,所以我有两次活动 .
这是我的代码:
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中存储变量以检查它是否已打开?
谢谢!
6 回答
您需要设置
Activity
的launchMode
属性,您将开始singleTop
. 这将导致传入的Intent传递到现有实例,而不是在Activity
已经位于任务堆栈顶部时启动新实例 .通过将
android:launchMode="singleTop"
添加到<activity>
元素,可以在清单中完成此操作 . 要访问最新的Intent(如果您对可能传入的任何数据感兴趣),请覆盖Activity
中的onNewIntent()
.尝试将标志设置为
Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP
.来自documentation for FLAG_ACTIVITY_CLEAR_TOP(强调我的):
使用
onNewIntent()
处理来自通知点击的新数据并刷新活动 .在
onNewIntent
中从新意图(由新通知提供服务)中获取新数据并捕获它们,例如:title = intent.getStringExtra("title")
在
onCreate
之前:)它将使用新的通知数据刷新当前活动 .
您也可以按照本教程:http://androidrace.com/2016/12/10/how-to-refresh-activity-on-new-notification-click-android-developer/
只需复制代码并将其粘贴到主启动器活动中即可 .
Here is Original Answer
我认为你应该添加一些逻辑来使它工作,也许这可以帮助:
For example I have a Splash Screen (Launcher and MAIN) of APP:
And in my FirebaseService I have done the following: