意图 - 如果活动正在运行,将其放在最前面,否则开始一个新的(来自通知) [英] Intent - if activity is running, bring it to front, else start a new one (from notification)

查看:32
本文介绍了意图 - 如果活动正在运行,将其放在最前面,否则开始一个新的(来自通知)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用有通知,很明显,没有任何标志,每次启动一个新活动,所以我让多个相同的活动在彼此之上运行,这是错误的.

My app has notifications, which - obviously - without any flags, start a new activity every time so I get multiple same activities running on top of each other, which is just wrong.

我想让它做的是将通知挂起意图中指定的活动带到前面,如果它已经在运行,否则启动它.

What I want it to do is to bring the activity specified in the notifications pending intent, to the front if it is already running, otherwise start it.

到目前为止,我所拥有的通知的意图/待定意图是

So far, the intent/pending intent for that notification I have is

private static PendingIntent prepareIntent(Context context) {
    Intent intent = new Intent(context, MainActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);

    return PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}

奇怪的是,它有时有效,有时却不起作用......我觉得我已经尝试了所有标志的组合.

and weirdly, it sometimes works, sometimes it doesn't... I feel like I've already tried every single combination of flags.

推荐答案

你可以使用这个:

<activity 
   android:name=".YourActivity"
   android:launchMode="singleTask"/>

它的工作原理类似于 "singleInstance" 但它不会有那种奇怪的动画.

which will work similar to "singleInstance" but it won't have that weird animation.

这篇关于意图 - 如果活动正在运行,将其放在最前面,否则开始一个新的(来自通知)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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