Android中如何控制活动堆栈/清除活动堆栈

发布于 2024-11-30 06:54:15 字数 593 浏览 1 评论 0原文

我有一个如下的应用程序(Home 是启动活动):

D
C
B
A
Home

我的流程如下:

用户从 Home 启动活动 A,该活动流向 BC。当用户离开活动 C 时,我希望销毁 ABC。也就是说,如果用户在活动 D 中按 BACK,它将返回到 Home

用户必须能够通过 ActivityABC 正常控制程序流程。因此,如果他们按下 ActivityC 中的后退按钮,则会返回到 Activity B

我查看了诸如 CLEAR_TOPNEW_TASK 之类的 Intent 标志,但它们似乎都没有执行我想要的操作。

我将不胜感激任何帮助!

I have an application that goes as follows (Home being the launch activity):

D
C
B
A
Home

My flow goes as follows:

The user starts activity A from Home, which flows to B and C. When the user leaves activity C, I want A, B, and C to be destroyed. That is, if the user presses BACK in activity D, it goes back to Home.

The user must be able to control program flow normally through activityA, B, and C. So if they press the back button in activityC, it goes back to activity B.

I've looked at Intent flags such as CLEAR_TOP and NEW_TASK, but none of them seem to do what I want them to.

I'd appreciate any help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我不在是我 2024-12-07 06:54:15

也许您正在寻找 FLAG_ACTIVITY_TASK_ON_HOME ?但它需要 API 级别 11 :(

对于 API 级别 <11,可以这样做:

启动活动 B 和 C 时,使用 startActivityForResult()。启动活动 D 时,执行以下操作:

startActivity(D);
setResult(KILL_YOURSELF); //KILL_YOURSELF is some arbitrary int that you use to identify that the other activities should exit
finish(); //finish the activity

这将杀死活动 C。然后在活动 A 中和B,像这样覆盖onActivityResult:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if(resultCode == KILL_YOURSELF) {
            setResult(KILL_YOURSELF);
            finish();
        }
    }

因此,活动B将完成,这反过来将触发A中的onActivityResult,因此它也将完成。

Perhaps you are looking for FLAG_ACTIVITY_TASK_ON_HOME? It requires API level 11 though :(

for API level <11, this can be done:

when starting activity B and C, use startActivityForResult(). When starting activity D, do this:

startActivity(D);
setResult(KILL_YOURSELF); //KILL_YOURSELF is some arbitrary int that you use to identify that the other activities should exit
finish(); //finish the activity

This will kill activity C. Then in activity A and B, override onActivityResult like this:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if(resultCode == KILL_YOURSELF) {
            setResult(KILL_YOURSELF);
            finish();
        }
    }

Thus activity B will finish, which in turn will trigger onActivityResult in A, so it will also finish.

最冷一天 2024-12-07 06:54:15

只需拦截 Activity D 中的“后退”按钮,并在拦截“后退”后按钮,转到“家庭活动”。当您前往家庭活动时,您可能/可能不想完成“D”活动。

Simply intercept the Back Button in Activity D, and upon intercepting the Back Button, head over to the Home Activity. You may / maynot want to finish 'D' activity as you are heading over to the Home Activity.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文