我在我的应用程序中遇到了一个bug,当它使用Google Play Store应用程序(以前称为Android Market)上的“打开”按钮启动时。从Play Store启动它似乎使用了不同的Intent
,而不是从手机的应用程序图标菜单启动它。这会导致启动同一活动的多个副本,这些副本彼此冲突。
例如,如果我的应用程序由活动A-B-C组成,则此问题可能导致A-B-C-A堆栈。
我尝试在所有活动上使用android:launchMode="singleTask"
来解决这个问题,但每当我点击主页按钮时,它都会产生不必要的副作用,即将活动堆栈清除到根目录。
预期行为为: A-B-C -> HOME ->,当应用程序恢复时,我需要: A-B-C -> HOME -> A-B-C
有没有好的方法可以防止在使用主页按钮时启动多个相同类型的活动,而不重置为根活动?
发布于 2011-10-13 09:23:52
将此代码添加到onCreate中,您就可以开始工作了:
// Possible work around for market launches. See https://issuetracker.google.com/issues/36907463
// 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;
}
}
发布于 2014-04-05 23:10:58
我将解释它失败的原因,以及如何以编程方式重现此错误,以便您可以将其合并到您的测试套件中:
通过Eclipse或Market应用程序启动应用程序时,它会使用意图标志启动:通过启动器( FLAG_ACTIVITY_NEW_TASK.
如果您希望在测试用例中重现此过程,请使用以下步骤:
adb shell am start -f 0x10000000 -n com.testfairy.tests.regression.taskroot/.MainActivity
然后执行其他活动所需的任何操作。出于我的目的,我只是放置了一个按钮来启动另一个活动。然后,使用以下命令返回启动器(主页):
adb shell am start -W -c android.intent.category.HOME -a android.intent.action.MAIN
并使用以下命令模拟通过launcher启动它:
adb shell am start -a "android.intent.action.MAIN" -c "android.intent.category.LAUNCHER" -f 0x10600000 -n com.testfairy.tests.regression.taskroot/.MainActivity
如果您没有合并isTaskRoot()解决方法,这将重现该问题。我们在自动测试中使用它来确保这个bug永远不会再次发生。
希望这能有所帮助!
发布于 2010-12-03 09:15:50
您是否尝试过singleTop启动模式?
以下是来自http://developer.android.com/guide/topics/manifest/activity-element.html的一些描述
...还可以创建"singleTop“活动的新实例来处理新的意图。但是,如果目标任务在其堆栈的顶部已经有一个活动的现有实例,则该实例将接收新的意图(在onNewIntent()调用中);不会创建新的实例。在其他情况下-例如,如果"singleTop“活动的现有实例在目标任务中,但不在堆栈的顶部,或者如果它在堆栈的顶部,但不在目标任务中-将创建一个新实例并将其推送到堆栈上。
https://stackoverflow.com/questions/4341600
复制相似问题