在启动另一个活动之前清除活动堆栈
你好(我的第一个问题在这里;-)
可能有类似的问题,但他们似乎都没有回答我的问题或给我一个真正的解决方案......
问题A
(root)启动B
。 B
可以启动其他活动。 A
决定关闭堆栈(网络连接丢失)并启动 C
。这似乎是一个重大决定,但用户会期望这一点,并且在这种情况下确实有意义......
问题
怎么做呢?我希望使用某个标志或者只是调用诸如 dismissStack
之类的东西。但我找不到正确的解决方案(请参阅下面的研究)。
研究
我阅读了很多文档,但可用的标志不符合我的需求(或者我没有理解它)。其他答案告诉我要记住已开始的活动(例如在数组中)并在需要时完成它们。但这对我来说似乎很肮脏,不知何故我不能接受这是正确的解决方案。我希望一定有更干净的东西?!
(存储堆栈对我来说似乎是错误的。您的应用程序可能会被系统杀死,因此您的静态数据,但您的历史记录将在启动时再次加载。所以您必须保留保存的堆栈。这会变得更加肮脏...... .)
更新
我的代码和我尝试过的内容(摘要):(
// in A
startActivityForResult(new Intent(this, B.class), REQUEST_B);
// network fail in A
// setting any flags on C has no influence
startActivityForResult(new Intent(this, C.class), REQUEST_C);
FLAG_ACTIVITY_CLEAR_TOP没有解决方案,请阅读文档...)
我按 C
上的 Back ,仍然弹出 B
并且仍然在那里...
我可以通过调用 finishActivity(REQUEST_B)
来删除该活动。但是,当 B
启动另一个活动 D
时,如果我稍后在 C
上点击 Back,它仍然会弹出。 B
消失了,但 D
仍然存在......
我确信有些东西我还不明白或只是错过了。如果真的必须这样的话,有人可以给我提示或批准我的研究吗?
Hi there (my first question here ;-)
There might be similar questions but none of them seem to answer my question or gives me a real solution...
ProblemA
(root) starts B
. B
can start other activities. A
decides to dismiss the stack (network connection loss) and start C
. This seems to be a drastic decision but the user would expect that and that really makes sense in this case...
Question
How to do that? I would expect using a certain flag or just call something like dismissStack
. But I can't find the right solution (see research below).
Research
I read a lot of documentation but the available flags don't match my needs (or I didn't understand it right). Other answers out there tell me to remember the started activities (e.g. in an array) and finish them when needed. But this seems dirty to me and somehow I can't accept that to be the right solution. There must be something cleaner I hope?!
(Storing the stack seems even wrong to me. Your app can be killed by the system, so your static data, but your history will be loaded again on startup. So you would have to persist your saved stack. This gets even dirtier...)
Update
My code and what I have tried (abstracted):
// in A
startActivityForResult(new Intent(this, B.class), REQUEST_B);
// network fail in A
// setting any flags on C has no influence
startActivityForResult(new Intent(this, C.class), REQUEST_C);
(FLAG_ACTIVITY_CLEAR_TOP is no solution, please read the documentation...)
I press Back on C
and B
still pops up and is so still there...
I can remove the activity by calling finishActivity(REQUEST_B)
. But when B
started another activity D
it will still pop up if I hit Back on C
later. B
is gone but D
is still there...
I'm sure there is something I didn't understand yet or just missed. Can someone give me a hint or approve my research if it has to be really that way...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为您正在寻找 FLAG_ACTIVITY_CLEAR_TOP 标志与你的意图。
从上面链接的文档中,提供了一个示例:
您可以在 Android 中查看有关任务和 Backstack 的更多信息 此链接
I think you are looking for the FLAG_ACTIVITY_CLEAR_TOP flag to be used with your intents.
From the documentation at the link above, an example is provided:
You can see more about Tasks and the Backstack in Android at this link in the Android Dev Docs
这就是我到目前为止所得到的,并且效果非常好:
使用
startActivityForResult
。如果您实际上不需要等待结果,请创建一个虚拟请求代码。然后在每个 Activity 的onDestroy
中执行此操作。例如:堆栈上有
A, B, C, D
,从A
调用finishActivity(REQUEST_B)
,这将链接code>onDestroy
调用,因此B
杀死C
并且C
杀死D
。这是我能想到的最好的解决方案。其他解决方案会扰乱活动生命周期和/或容易出错。
我仍然希望有一种更清洁的方法......
That's what I got so far and it works pretty good:
Use
startActivityForResult
. Create a dummy request code if you actually don't need to wait for a result. Then do this inonDestroy
of every activity.For example: You have
A, B, C, D
on the stack, callfinishActivity(REQUEST_B)
fromA
, this will chain theonDestroy
calls soB
killsC
andC
killsD
.That's the best solution I can think of. Other solutions out there are messing with the activity lifecycle and/or are error prone.
I still hope there's a cleaner way...
您可以使用新任务和清除任务的标志来清除堆栈中的所有先前活动
You can use both the flags for new task and clear task to clear all previous activities from the stack