在启动另一个活动之前清除活动堆栈

发布于 2024-12-10 12:33:53 字数 1385 浏览 5 评论 0原文

你好(我的第一个问题在这里;-)

可能有类似的问题,但他们似乎都没有回答我的问题或给我一个真正的解决方案......


问题
A(root)启动BB 可以启动其他活动。 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...


Problem
A (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 技术交流群。

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

发布评论

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

评论(3

假装不在乎 2024-12-17 12:33:53

我认为您正在寻找 FLAG_ACTIVITY_CLEAR_TOP 标志与你的意图。

从上面链接的文档中,提供了一个示例:

例如,考虑一个由以下活动组成的任务:A、B、C、D。如果 D 使用解析为活动 B 的组件的 Intent 调用 startActivity(),则 C 和 D 将完成并且 B 接收给定的意图,导致堆栈现在为:A,B。

您可以在 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:

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.

You can see more about Tasks and the Backstack in Android at this link in the Android Dev Docs

眼眸里的快感 2024-12-17 12:33:53

这就是我到目前为止所得到的,并且效果非常好:

使用 startActivityForResult。如果您实际上不需要等待结果,请创建一个虚拟请求代码。然后在每个 Activity 的 onDestroy 中执行此操作。

if (isFinishing()) {
    finishActivity(REQUEST_CODE);
}

例如:堆栈上有 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 in onDestroy of every activity.

if (isFinishing()) {
    finishActivity(REQUEST_CODE);
}

For example: You have A, B, C, D on the stack, call finishActivity(REQUEST_B) from A, this will chain the onDestroy calls so B kills C and C kills D.


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...

寻找我们的幸福 2024-12-17 12:33:53

您可以使用新任务和清除任务的标志来清除堆栈中的所有先前活动

i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

You can use both the flags for new task and clear task to clear all previous activities from the stack

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