通过Flag进行的任何活动都可以返回MainActivity [英] Go back to MainActivity from any activities with Flag

查看:132
本文介绍了通过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 - A not as document wrote:

FLAG_ACTIVITY_NEW_TASK
在新任务中开始活动.如果任务是已经为您现在开始的活动运行,该任务是还原到最后状态恢复到前台,并且活动在onNewIntent()中接收到新的意图.

FLAG_ACTIVITY_NEW_TASK
Start the activity in a new task. If a task is already running for the activity you are now starting, that task is brought to the foreground with its last state restored and the activity receives the new intent in onNewIntent().

这会产生与"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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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