Android从后台堆栈中删除活动 [英] Android remove Activity from back stack

查看:21
本文介绍了Android从后台堆栈中删除活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我有点不知道如何处理这个问题.所以我有 MainActivity,可以从那里启动一个 Activity 到 DegreePlanActivity,然后从那里启动另一个 Activity 到 EditDegreePlan.我在 AndroidManifest 中将 EditDegreePlan 设置为 noHistory.问题是在他们保存 EditDegreePlan 后,它会向 DegreePlan 启动一个活动.因此,如果用户按 Back,他们必须按两次才能再次进入 MainActivity.我想摆脱它,所以他们只需要按一次.不过,我对如何做到这一点感到困惑.

Okay so I'm kind of stumped on what to do with this. So I have the MainActivity, and from there an Activity can be launched to DegreePlanActivity, and from there another Activity can be launched to EditDegreePlan. I've got EditDegreePlan set to noHistory in the AndroidManifest. The problem is after they save the EditDegreePlan it launches an Activity to DegreePlan. So if the user presses Back they have to press it twice to get to MainActivity again. I want to get rid of that so they only have to press it once. I'm stumped on how to do this though.

如果我将 DegreePlanActivity 设置为 noHistory,那么在 EditDegreePlan 中他们无法按 Back to it.

If I set DegreePlanActivity to noHistory then they couldn't press Back to it while in EditDegreePlan.

我已经尝试覆盖 onBackPressed 方法并启动 MainActivity 的意图.那么问题是他们必须多次按 Back 才能退出应用程序.

I've tried overriding onBackPressed method and launching an intent to MainActivity. The problem then is that they have to press Back multiple times to exit the app then.

我该怎么办?

推荐答案

FLAG_ACTIVITY_CLEAR_TOP清空你的Activity栈,你可以使用下面的代码:

FLAG_ACTIVITY_CLEAR_TOP clears your Activity stack , you can use the code below:

Intent intent = new Intent(this, Activity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

请记住,此标志仅清除中间活动,例如,如果您的 Back Stack 中有 A、B、C,则使用此标志从 C Activity 转到 D,则 不会 clear Back Stack 并且堆栈将是 A,B,C,D 但是如果您使用此标志从 Activity D 转到 Activity A ,则 B,C,D 活动将从堆栈,您将只有 A 在 Back Stack 中.

Remember that this flag clears just Intermediate Activities , for example if you have A,B,C in your Back Stack then going from C Activity to D with this flag this does not clear Back Stack and the Stack would be A,B,C,D but if you go from Activity D to Activity A with this flag , B,C,D Activities will pop up from the stack and you will have just A in the Back Stack .

这篇关于Android从后台堆栈中删除活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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