gpt4 book ai didi

android - 子 Activity 在向上导航时被销毁

转载 作者:行者123 更新时间:2023-11-30 02:03:38 27 4
gpt4 key购买 nike

我有 3 个 Activity :A、B 和 C,它们代表 Web 表单的 3 个页面。 A 是 B 的父 Activity ,B 是 C 的父 Activity 。我已经使用 android:launchMode="singleTop" 正确实现了向上导航。当我有信息是A,信息在B,信息在C,我在Activity C的时候点击返回,Activity B的信息就出现了。然后我再次点击返回,Activity a 的信息就出现了。

当我从 Activity A 转到 Activity B 时,问题就出现了。当然,我希望保存在第二页的信息仍然存在,但它重新创建了 Activity B,所有信息都丢失了。如何使 Activity B 恢复并且不让 Activity 被破坏。我在文档中读到,当使用向上导航时,当前 Activity 被销毁,后退堆栈中的 Activity 被恢复。我不知道如何实现这个。

非常感谢任何帮助。

编辑:我想要的更直观的表示。这是来自文档:

enter image description here

如图所示,当当前 Activity 为 Activity 3 时按下返回键,Activity 3 将被销毁。我不希望这发生。能否保存 Activity 3 以便稍后恢复?

最佳答案

您的解决方案来自 docs

Similarly, if you navigate up to an activity on the current stack, the behavior is determined by the parent activity's launch mode. If the parent activity has launch mode singleTop (or the up intent contains FLAG_ACTIVITY_CLEAR_TOP), the parent is brought to the top of the stack, and its state is preserved. The navigation intent is received by the parent activity's onNewIntent() method. If the parent activity has launch mode standard (and the up intent does not contain FLAG_ACTIVITY_CLEAR_TOP), the current activity and its parent are both popped off the stack, and a new instance of the parent activity is created to receive the navigation intent.

回答你的总体问题NO

你从AB;然后从 B 返回到 A 并且您希望保留 B ?回到 B 继续你的任务的概率是多少——所以它会被杀死并重新启动。

要提供某种解决方案,请使用已弃用的 ActivityGroup 或(我不确定这是否有效)-将 launchmode="singleTop" 更改为 singleInstance 并覆盖它的 onBackPressed() 以启动一个新的 Activity 调用触发它的 Activity - 你可以通过 getIntent() 获取触发它的 Activity .. 这里的逻辑是 singleInstance 驻留在不同的任务中并且不允许其中的其他 Activity ,所以它杀死它的条款,它不会死,当一个新的 Activity 被激发时,并且因为你的接收 Activity 有它的 lauchmode 和它的根你那里没有问题。覆盖 onBackPressed 是为了防止正常流动..

希望这次能有所帮助,但总体而言

关于android - 子 Activity 在向上导航时被销毁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31107156/

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