Android中如何控制活动堆栈/清除活动堆栈
我有一个如下的应用程序(Home 是启动活动):
D
C
B
A
Home
我的流程如下:
用户从 Home
启动活动 A
,该活动流向 B
和 C
。当用户离开活动 C
时,我希望销毁 A
、B
和 C
。也就是说,如果用户在活动 D
中按 BACK
,它将返回到 Home
。
用户必须能够通过 ActivityA
、B
和 C
正常控制程序流程。因此,如果他们按下 ActivityC
中的后退按钮,则会返回到 Activity B
。
我查看了诸如 CLEAR_TOP
和 NEW_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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许您正在寻找
FLAG_ACTIVITY_TASK_ON_HOME
?但它需要 API 级别 11 :(对于 API 级别 <11,可以这样做:
启动活动 B 和 C 时,使用 startActivityForResult()。启动活动 D 时,执行以下操作:
这将杀死活动 C。然后在活动 A 中和B,像这样覆盖onActivityResult:
因此,活动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:
This will kill activity C. Then in activity A and B, override onActivityResult like this:
Thus activity B will finish, which in turn will trigger onActivityResult in A, so it will also finish.
只需拦截 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.