gpt4 book ai didi

Android清除/完成以前的 Activity ,除了一个

转载 作者:太空宇宙 更新时间:2023-11-03 10:20:26 25 4
gpt4 key购买 nike

在android中我有以下路径:

Activity 1 -> Activity 2 -> Activity 3 -> ... Activity N -> 按下按钮 ...

当按下按钮时,我想清除/完成从 Activity 2 到 N 的所有 Activity ,然后转到 Activity X。换句话说,我想完成所有 Activity ,直到最初的一个,然后移动到另一个。

如果我使用标志:

CLEAR_TOP、CLEAR_TASK、NEW_TASK 等

从理论上讲,它会用第一个 Activity 完成所有以前的 Activity 。有没有办法让第一个 Activity 保持活跃并转移到 Activity X?

最佳答案

我不确定我是否理解这个问题。或者更确切地说,无法使用 FLAG_ACTIVITY_CLEAR_TOP 的原因。

If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.

For example, consider a task consisting of the activities: A, B, C, D. If D calls startActivity() with an Intent that resolves to the component of activity B, then C and D will be finished and B receive the given Intent, resulting in the stack now being: A, B.

如果我没理解错的话,这正是你想要的。特别是对于,

Activity 1 -> Activity 2 -> Activity 3 -> ... Activity N -> press button ...

使用 Activity2.class 标志启动 Intent FLAG_ACTIVITY_CLEAR_TOP | FLAG_ACTIVITY_SINGLE_TOP 将返回到 Activity2 的现有实例(而不是创建一个新实例)并在 onNewIntent() 中传递新 Intent 。您只需要为此 Intent 添加一个额外的内容,这样该方法就会知道它应该在之后调用 ActivityX。

也就是说,除非我遗漏了什么。 :)

关于Android清除/完成以前的 Activity ,除了一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24026051/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com