安卓:清除背部栈 [英] Android: Clear the back stack

查看:148
本文介绍了安卓:清除背部栈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Android的,我有一些活动,让我们说A,B,C。

In Android I have some activities, let's say A, B, C.

在一个我用这个code打开B:

In A I use this code to open B:

Intent intent = new Intent(this, B.class);
startActivity(intent);

在B I使用code打开C:

In B I use this code to open C:

Intent intent = new Intent(this, C.class);
startActivity(intent);

当用户点击一个按钮,在CI要回到A疏通背部栈(关闭这两个B和C​​)。因此,当用户使用后退按钮B和C都不会显示出来,我一直在尝试以下操作:

When the user taps a button in C I want to go back to A and clear the back stack (close both B and C). So when the user use the back button B and C will not show up, I've been trying the following:

Intent intent = new Intent(this, A.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
startActivity(intent);

不过,B和C仍呈现了,如果我使用返回按钮,当我回到活动答:我怎样才能避免这种情况?

But B and C are still showing up if I use the back button when I'm back in activity A. How can I avoid this?

推荐答案

尝试添加<一个href="http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_NEW_TASK"><$c$c>FLAG_ACTIVITY_NEW_TASK如在文档描述的<一个href="http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP"><$c$c>FLAG_ACTIVITY_CLEAR_TOP:

此发射模式也可用于   联用效果好   FLAG_ACTIVITY_NEW_TASK:如果用来   启动一个任务的根活动,它   会带来什么当前正在运行   该任务的实例   前台,然后将其清除其   根状态。这是特别有用,   例如,启动一个时   从通知活动   经理。

This launch mode can also be used to good effect in conjunction with FLAG_ACTIVITY_NEW_TASK: if used to start the root activity of a task, it will bring any currently running instance of that task to the foreground, and then clear it to its root state. This is especially useful, for example, when launching an activity from the notification manager.

所以,你的code,推出 A 是:

So your code to launch A would be:

Intent intent = new Intent(this, A.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); 
startActivity(intent);
finish(); // call this to finish the current activity

这篇关于安卓:清除背部栈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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