gpt4 book ai didi

Android 创建新的应用程序实例

转载 作者:行者123 更新时间:2023-11-29 22:26:48 25 4
gpt4 key购买 nike

我有一个包含多个 Activity 的应用程序。自己的 Application 类用于存储全局数据,因此任何 Activity 都可以检索它。问题是当我在 Activity 中艰难地走动时(尤其是通过创建大量位图图像的 Activity ),系统会使用不正确的全局数据创建 Application 类的新实例。

问题是:为什么会这样? )

我相信对这种行为有一些简单的解释。

最佳答案

您应该在 androidManifest.xml 中的 activity 标记内指定 android:launchMode 属性以满足您的需要,例如:

<activity android:launchMode="singleTask" [...]>
[...]
</activity>

您可以阅读更多相关信息: android:launchMode=["multiple" | "singleTop" | "singleTask" | "singleInstance"]

"singleTask" and "singleInstance" activities can only begin a task. They are always at the root of the activity stack. Moreover, the device can hold only one instance of the activity at a time — only one such task.

The "singleTask" and "singleInstance" modes differ from each other in only one respect: A "singleTask" activity allows other activities to be part of its task. It's always at the root of its task, but other activities (necessarily "standard" and "singleTop" activities) can be launched into that task. A "singleInstance" activity, on the other hand, permits no other activities to be part of its task. It's the only activity in the task. If it starts another activity, that activity is assigned to a different task — as if FLAG_ACTIVITY_NEW_TASK was in the intent.

这样您就可以确保只有一个 Activity 实例在运行。

关于Android 创建新的应用程序实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5674473/

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