Android:清除后退堆栈 [英] Android: Clear the back stack

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

问题描述

在 Android 中,我有一些活动,比如 A、B、C.

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

在 A 中,我使用此代码打开 B:

In A, I use this code to open B:

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

在B中,我用这个代码打开C:

In B, I use this code to open C:

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

当用户点击 C 中的按钮时,我想返回 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);

但是,如果我在返回活动 A 时使用后退按钮,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?

推荐答案

尝试添加 FLAG_ACTIVITY_NEW_TASKFLAG_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.

所以你启动 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);
CurrentActivity.this.finish(); // if the activity running has it's own context


// view.getContext().finish() for fragments etc.

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

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