Android 应用程序 class 生命周期文档

Android Application class lifecyle documentation

我正在尝试查找有关 Android Application class 生命周期的官方文档。显然,对于我在 Whosebug here and here the Application class can be killed if the system needs memory. Even this 教程中找到的内容,是这样说的。

但是有几件事让我有点恼火:

多谢指教

据我所知,您无法处理应用程序终止事件。这是应用程序的 onTerminate 方法的引用:

This method is for use in emulated process environments. It will never be called on a production Android device, where processes are removed by simply killing them; no user code (including this callback) is executed when doing so.

一般的想法是您不应该关心应用程序是否被终止。如果是,OS 将在下次需要时重新启动应用程序,否则它将恢复(您将使用 Activity / 片段生命周期事件来实现此目的)。

您需要存储什么数据 - 是否可以更早地存储它(当从网络服务等接收到数据时)而不是等到最后一刻?

I can't find an official documentation telling me that yes, the Application class can be killed on low memory.

以下是对声明位置的引用:

I can't find any official diagram representing the Application lifecycle neither.

这是一个合理的观察...虽然以下是基于观点的,但我最好的猜测是这样的图表会与上一节中描述的 Android 的多任务处理 "philosophy" 相矛盾提供的参考资料:

"A key to how Android handles applications in this way is that processes don't shut down cleanly. When the user leaves an application, its process is kept around in the background, allowing it to continue working (for example downloading web pages) if needed, and come immediately to the foreground if the user returns to it. If a device never runs out of memory, then Android will keep all of these processes around, truly leaving all applications "运行”一直都是。”

I can't find any proper callback to use when the Application class is killed excepted onLowMemory(). Does it mean that I have to use this method to persist my data?

关于onLowMemory()的描述很简单,我们是在谈论后台进程还是前台进程UI?...

如果应用程序 Activities 的 none 在前台并且 OS 内存不足,它可能会终止应用程序,以便 [=13= 的 none ] 或应用组件的 (Activity, Service) 回调将被调用。也就是说,(因为您正在处理 Activities)我建议按照文档将所有持久数据存储在 onPause().

If the Application class is killed on low memory pressure and the app comes to foreground again, how can I know in its onCreate() that the app has been recreated after a system kill?

你无法在ApplicationonCreate()中识别它。