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

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

问题描述

我有通知打开一定的活动,如果我按他们的应用程序。我想,如果我点击的通知和活动已经打开,它的不可以再次启动,但只带到前面。

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_FRONT FLAG_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.

这是我的code:

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);

我可以与标志管理它或者我应该存储在共享preferences一个变量来检查它是否开启或不?

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

谢谢!

推荐答案

您需要设置活动的 launchMode 属性你开始 singleTop 。这将导致输入的意图被输送到现有的实例,而不是开始一个新的实例时活性已经是在任务的堆栈的顶部。

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.

这是在清单中加入机器人完成:launchMode =singleTop<活性GT; 元件。要访问最新的意图(如果你有兴趣,可能已通过与它的任何数据),覆盖 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天全站免登陆