Android Application类Lifecyle文档 [英] Android Application class lifecyle documentation

查看:132
本文介绍了Android Application类Lifecyle文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找有关Android Application类生命周期的官方文档.显然,对于我在StackOverflow上找到的内容此处 Application 类可以是如果系统需要内存,则终止.甚至教程也是如此.

I'm trying to find the official documentation about the Android Application class lifecycle. Apparently, for what I found on StackOverflow here and here the Application class can be killed if the system needs memory. Even this tutorial says so.

但是,有几件事让我有些恼火:

But few things irritates me a bit about this:

  • 我找不到官方文档告诉我,是的,Application类可以在内存不足时被杀死.
  • 我也找不到任何代表Application生命周期的正式图表.
  • 除了
  • 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.
  • I can't find any proper callback to use when the Application class is killed except onLowMemory(). Does it mean that I have to use this method to persist my data?
  • 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? In an Activity I would test the savedInstanceState, but as far as I know there is nothing similar in the Application class.

感谢您的启发.

推荐答案

我找不到官方文档告诉我,是的,Application类可以在内存不足的情况下被杀死.

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

下面是对其声明的引用:

Below are the references to where it's been stated:

  • Application Fundamentals
  • Processes and Threads
  • Multitasking the Android Way

我也找不到任何代表应用程序生命周期的正式图表.

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

这是一个合理的观察...尽管以下内容基于观点,但我最大的猜测是,这种图表将与提供的最新参考资料中所述的Android多任务哲学"相抵触.

This is a reasonable observation... Although the following is opinion-based, my best guess is that such a diagram would contradict the Android's multitasking "philosophy" as described in the last reference provided:

""Android如何以这种方式处理应用程序的一个关键是进程不会干净地关闭.当用户离开应用程序时,其进程将保留在后台,从而使其能够继续工作(对于例如下载网页)(如果需要的话),如果用户返回它,则立即显示在前台.如果设备永远不会用完内存,那么Android会保留所有这些进程,真正让所有应用程序运行"所有时间."

onLowMemory()之外,当Application类被杀死时,我找不到要使用的任何适当的回调.这是否意味着我必须使用此方法来保存数据?

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?...

Regarding onLowMemory() whose description is quite straightforward, are we talking about a background process or foreground UI?...

如果应用程序Activities都不在前台并且操作系统内存不足,则它可能会杀死该应用程序,因此Application或应用程序组件的(ActivityService)回调都不会被调用.就是说,(因为您正在处理Activities)我建议根据文档将所有持久性数据存储在

If none of application Activities is in foreground and OS is low on memory, it may kill the app so that none of the Application's or the app component's (Activity, Service) callbacks will be invoked. That said, (since you're dealing with Activities) I recommend to store all persistent data as per the documentation, in onPause().

如果Application类在低内存压力下被杀死,并且该应用再次出现在前台,我怎么能在其onCreate()中知道该应用在系统被杀死后被重新创建?

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?

您无法在 Application 的目录中识别出它onCreate().

这篇关于Android Application类Lifecyle文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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