意图 - 如果活动是在运行,把它前面,否则启动一个新的(从通知) [英] Intent - if activity is running, bring it to front, else start a new one (from notification)

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

问题描述

我有一个地狱这样做的,我只是不能看着办吧。 我的应用程序有通知,这是 - 很明显 - 不带任何标志,每次都让我得到彼此的顶部运行多个相同的活动,这是错误的开始一个新的活动

I am having a hell doing this and I just cant figure it out. 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 its 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 doesnt .. I feel like Ive 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天全站免登陆