应用程序在后台被杀死时的应用程序生命周期 [英] Application lifecycle when app is killed in the background

查看:895
本文介绍了应用程序在后台被杀死时的应用程序生命周期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到的一个问题的一些背景知识:在我的应用程序中,我有一个单例对象,我经常使用该对象访问网络呼叫的ID和令牌之类的东西.有时,当应用程序在后台被杀死时,此单例会丢失其状态.但是,当再次打开应用程序并在启动器Activity之后的某个Activity中启动该应用程序时,单例为null.

Some background on a problem that I have been encountering: in my app I have a singleton object that I use regularly to access things like id and token for network calls. Sometimes when the app is killed in the background, this singleton loses its state. However, when the app is opened again and starts up in some Activity past the launcher Activity, the singleton is null.

我正在重构它,但是一直在苦恼如何确保即使在应用重启时单例也总是存在,但是我不确定 Android 在应用从后台重启.

I am in the process of refactoring this, but have been agonizing over how to ensure that the singleton will always be present even in app restart, but I'm not sure what Android does when the app restarts from being backgrounded.

我浏览了我们在应用程序中使用的某些库( Facebook Intercom )中的源代码,以了解它们如何管理单例以及为什么它们的静态变量看起来像永远存在,并产生了一种理论.

I went through source code in some of the libraries we use in the app (Facebook, Intercom) to see how they manage their singletons and why their static variables seemed to just always be present, and came upon a theory.

因此,在正常的应用程序冷启动中,应用程序的行为如下:

So on a normal app cold start, the app behaves like this:

Application.onCreate() -> Launcher.onCreate() -> Activity A -> Activity B

假设用户在Activity B中,并且使应用程序后台运行.使用其他一些应用程序后,它们又回到了我的应用程序中,但在此之间的某个时间点已被杀死.然后,生命周期变为:

Say the user was in Activity B and backgrounds the app. After using some other apps, they come back to my app but it has been killed at some point in between. The lifecycle then becomes this:

Application.onCreate() -> Activity B

我认为问题在于我在启动器Activity中初始化了单例,因此,当B尝试从单例中获取值时,它会变为null.如果我在Application.onCreate()中初始化单例,它将在再次拉起应用程序时始终被初始化.这样对吗?

I think the problem is that I initialize the singleton in the launcher Activity, 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?

TL; DR被杀死并再次带到前台的应用程序将始终调用其Application.onCreate(),然后直接转发到它所在的Activity.因此,对于应用程序功能至关重要的应用程序初始化应该存在于Application onCreate()中.

TL;DR An 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().

推荐答案

被杀死并再次带到前台的应用程序将始终调用其Application.onCreate(),然后直接转发到它所在的活动.因此,对于应用程序功能至关重要的应用程序初始化应该存在于Application onCreate()中.

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

正确.

我认为问题在于我在LauncherActivity中初始化了单例,因此,当B尝试从单例中获取值时,它会变为null.如果我在Application.onCreate()中初始化Singleton,则将在再次拉起应用程序时始终对其进行初始化.这是正确的吗?

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?

在您的情况下,问题确实出在其中.但是,如果通过它变成空值" 来表示单例实例为null,则这不是单例应该如何工作的方式.无论从何处调用单例方法,其实例都不应为null.

In your case the problem is really in that. However, if by "it comes up null" you mean the singleton instance is null then it is not how a singleton is supposed to work. No matter where you call singleton's methods from, its instance should not be null.

这篇关于应用程序在后台被杀死时的应用程序生命周期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆