应用程序在后台被杀死时的应用程序生命周期
Application lifecycle when app is killed in the background
关于我遇到的问题的一些背景知识:在我的应用程序中,我有一个单例对象,我经常使用它来访问网络调用的 id 和令牌之类的东西。有时当应用程序在后台被杀死时,这个单例会失去它的状态。但是,当应用程序再次打开并在启动器 Activity
之后的某个 Activity
中启动时,单例为 null。
我正在对此进行重构,但一直在苦恼如何确保即使在应用程序重启时单例也始终存在,但我不确定 Android 当应用程序从后台重新启动时执行。
我查看了我们在应用程序中使用的一些库的源代码(Facebook、Intercom)以了解它们如何管理他们的单身人士以及为什么他们的静态变量似乎总是存在,并提出了一个理论。
所以在正常的应用冷启动中,应用的行为如下:
Application.onCreate() -> Launcher.onCreate() -> Activity A -> Activity B
假设用户在 Activity
B 中并设置了应用程序的后台。使用其他一些应用程序后,他们会回到我的应用程序,但它已在中间的某个时刻被杀死。然后生命周期变成这样:
Application.onCreate() -> Activity B
我认为问题在于我在启动器 Activity
中初始化了单例,结果,当 B 试图从单例中获取一个值时,它出现了 null。如果我在Application.onCreate()
中初始化单例,再次拉起app时总会初始化。这个对吗?
TL;DR 被杀死并再次进入前台的应用程序将始终调用其 Application.onCreate()
,然后直接转发到它所在的 Activity
。因此,对应用程序运行至关重要的应用程序初始化应该位于 Application
onCreate()
中。
是的,当应用程序进入前台时,总是会调用 Application.onCreate()
,因此您需要初始化的每个 Singleton 都必须存在。
A application that is killed and brought to foreground again will always call its Application.onCreate() and then forward directly to the activity it was on. Therefore, app initializations that are critical to the app functioning should live in the Application onCreate().
正确。
I think the problem is that I initialize the singleton in the LauncherActivity, and as a result, when B tries to get a value from the singleton, it comes up null. If I initialize the Singleton in Application.onCreate(), it will always be initialized when the app is pulled up again. Is this correct?
你的情况确实是问题所在。但是,如果 "it comes up null" 你的意思是单例实例是 null
那么这不是单例应该如何工作的。无论你从哪里调用单例的方法,它的实例都不应该是null
.
关于我遇到的问题的一些背景知识:在我的应用程序中,我有一个单例对象,我经常使用它来访问网络调用的 id 和令牌之类的东西。有时当应用程序在后台被杀死时,这个单例会失去它的状态。但是,当应用程序再次打开并在启动器 Activity
之后的某个 Activity
中启动时,单例为 null。
我正在对此进行重构,但一直在苦恼如何确保即使在应用程序重启时单例也始终存在,但我不确定 Android 当应用程序从后台重新启动时执行。
我查看了我们在应用程序中使用的一些库的源代码(Facebook、Intercom)以了解它们如何管理他们的单身人士以及为什么他们的静态变量似乎总是存在,并提出了一个理论。
所以在正常的应用冷启动中,应用的行为如下:
Application.onCreate() -> Launcher.onCreate() -> Activity A -> Activity B
假设用户在 Activity
B 中并设置了应用程序的后台。使用其他一些应用程序后,他们会回到我的应用程序,但它已在中间的某个时刻被杀死。然后生命周期变成这样:
Application.onCreate() -> Activity B
我认为问题在于我在启动器 Activity
中初始化了单例,结果,当 B 试图从单例中获取一个值时,它出现了 null。如果我在Application.onCreate()
中初始化单例,再次拉起app时总会初始化。这个对吗?
TL;DR 被杀死并再次进入前台的应用程序将始终调用其 Application.onCreate()
,然后直接转发到它所在的 Activity
。因此,对应用程序运行至关重要的应用程序初始化应该位于 Application
onCreate()
中。
是的,当应用程序进入前台时,总是会调用 Application.onCreate()
,因此您需要初始化的每个 Singleton 都必须存在。
A application that is killed and brought to foreground again will always call its Application.onCreate() and then forward directly to the activity it was on. Therefore, app initializations that are critical to the app functioning should live in the Application onCreate().
正确。
I think the problem is that I initialize the singleton in the LauncherActivity, and as a result, when B tries to get a value from the singleton, it comes up null. If I initialize the Singleton in Application.onCreate(), it will always be initialized when the app is pulled up again. Is this correct?
你的情况确实是问题所在。但是,如果 "it comes up null" 你的意思是单例实例是 null
那么这不是单例应该如何工作的。无论你从哪里调用单例的方法,它的实例都不应该是null
.