Android SingleTask启动模式与Home键的问题

我们知道SingleTask模式对于Activity的影响如下:

例如我们将Activity A设置为SingleTask模式,其他的Acitivity设置为Standard模式,则有以下效果:

A -> B -> C -> D 我们将Activity A一直跳转到Activity D,如果此时D -> A,则会发生Activity B,C,D都会被pop出栈中Activity A则会显示到栈顶。

我利用这个特性时,处理单端登录:将将Activity A设置成SingleTask模式,并设置成启动的Acitivity,

如果后台告知账号在其他地方登录了,则此时直接将跳转Activity A即可,其他的Acitivity都会被pop出去。

现在问题就来了,我们将App的启动的MainActivity设置为SingleTask模式时,如果我们在App跳转到Activity B时,按下Home键,此时点击App Icon,

此时不是显示Activity B,还是直接会显示Activity A,即是把A上面的Activity都会被pop出去,Activity A显示到栈顶,但这不是我想要的。

问题描述:

My main activity A has as set android:launchMode="singleTask" in the manifest. Now, whenever I start another activity from there, e.g. B and press the HOME BUTTON on the phone to return to the home screen and then again go back to my app, either via pressing the app's button or pressing the HOME BUTTONlong to show my most recent apps it doesn't preserve my activity stack and returns straight to A instead of the expected activity B.

Here the two behaviors:

Expected: A > B > HOME > B
Actual: A > B > HOME > A (bad!)

如何处理呢:

扫描二维码关注公众号,回复: 7218142 查看本文章

Just remove the launchMode="singleTask" and set FLAG_ACTIVITY_CLEAR_TOP|FLAG_ACTIVITY_SINGLE_TOP flag whenever call an intent to A

链接:https://stackoverflow.com/questions/2417468/android-bug-in-launchmode-singletask-activity-stack-not-preserved

猜你喜欢

转载自www.cnblogs.com/hbolin/p/11479019.html