本文介绍了通过Flag进行的任何活动都可以返回MainActivity的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从任何活动返回MainActivity.例如,我的活动堆栈: A-B-C-D.A(MainActivity)在堆栈的底部,D在堆栈的顶部.

I want to go back to MainActivity from any activities.For ex, my stack of activities:A - B - C - D. A (MainActivity) is in the bottom and D is in the top of stack .

当我使用 android:launchMode ="singleTask" 时.我可以按预期在任何活动中返回A.

When I use android:launchMode="singleTask" . I can go back to A at any actvities as I expected.

但是当我使用标志 FLAG_ACTIVITY_NEW_TASK (没有launchMode ="singleTask")时,它无法按预期运行,它将打开一个新的Activity.堆栈是: A-B-C-D-A 不像文件中写的那样:

But when I use flag FLAG_ACTIVITY_NEW_TASK (without launchMode="singleTask"), it does not work as expected, it open a new Activity. And stacks are: A - B - C - D - Anot as document wrote:

这会产生与"singleTask" launchMode值相同的行为,在上一节.

This produces the same behavior as the "singleTask" launchMode value, discussed in the previous section.

有人知道原因吗?谢谢.

Anyone knows the reason? Thanks.

推荐答案

我希望此解决方案可以解决您的问题::-仅将其设置为 Intent

I hope this solution solve your problem::- Only set this to Intent

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);

这篇关于通过Flag进行的任何活动都可以返回MainActivity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 20:07