如何prevent一个活动的多个实例,当它与不同的意图发动 [英] How to prevent multiple instances of an activity when it is launched with different intents

查看:171
本文介绍了如何prevent一个活动的多个实例,当它与不同的意图发动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一个错误,在我的应用程序时,它使用了Android市场上的打开按钮启动。看来,从市场空空它采用了不同的意图,然后从手机的应用程序菜单中启动它。这导致了同一活动的多个副本正在启动,这是相互冲突。

I've come across a bug in my application when it is launched using the "Open" button on the android market. It seems that lauching it from the market uses a different intent then launching it from the phone's applications menu. This is leading to multiple copies of the same activity being launched, which are conflicting with each other.

例如,如果我的应用程序包括活动的ABC则上述问题可能导致栈ABCA。

For example, if my app consists of the activities A-B-C then the above issue can lead to a stack A-B-C-A.

我尝试使用机器人:launchMode =singleTask上的所有活动,以解决这个问题,但它具有清除activiy栈根每当我诚惶诚恐的不必要的副作用。例: ABC - >首页 - >一个,当我需要的是ABC - >首页 - > ABC

I tried using android:launchMode="singleTask" on all the activities to fix this problem, but it has the unwanted side-effect of clearing the activiy stack to root whenever I hit HOME. Example: A-B-C -> HOME -> A when what I need is A-B-C -> HOME -> A-B-C

有没有一种好方法prevent推出相同类型的多个活动,而无需使用在家的时候给CH375复位根系活力?

Is there a good way to prevent launching multiple activities of the same type without reseting to the root activity when using HOME?

推荐答案

这加入的onCreate,你应该是好去:

Add this to onCreate and you should be good to go:

// Possible work around for market launches. See http://code.google.com/p/android/issues/detail?id=2373
// for more details. Essentially, the market launches the main activity on top of other activities.
// we never want this to happen. Instead, we check if we are the root and if not, we finish.
if (!isTaskRoot()) {
    final Intent intent = getIntent();
    if (intent.hasCategory(Intent.CATEGORY_LAUNCHER) && Intent.ACTION_MAIN.equals(intent.getAction())) {
        Log.w(LOG_TAG, "Main Activity is not the root.  Finishing Main Activity instead of launching.");
        finish();
        return;       
    }
}

这篇关于如何prevent一个活动的多个实例,当它与不同的意图发动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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