Android应用程序生命周期澄清

Android application lifecycle clarification

任何人都可以确认以下关于Android应用程序生命周期?

1)当应用程序在前台时,内存将包含Application对象的实例,所有活动的实例(未被杀死)以及从其中一个根引用的所有对象引用(尚未被垃圾回收)

2)当应用程序进入后台时,Android Framework可以在某些时候:
a)杀死给予应用程序目的的整个过程,这将完全删除内存中的所有对象
b)只杀死(所以很难删除其他对象引用)活动(通过完成它们,实质上是任何碎片)保存它们的状态并创建活动堆栈并留下任何其他东西(应用程序对象,任何其他静态对象,可以从任何根访问的引用。

我最感兴趣的是2b,但是我会对所有这些点进行确认,因为我试图从头到尾抓住整个概念。


1) When application is in foreground the memory will contain instance
of the Application object, instances of all activities (not killed)
and all the object references that are referenced from one of the
root's (haven't been garbage collected)

1
2
3
4
5
> There will only ever be a few such processes in the system, and these
> will only be killed as a last resort if memory is so low that not even
> these processes can continue to run. Generally, at this point, the
> device has reached a memory paging state, so this action is required
> in order to keep the user interface responsive.

2) When application goes to background, at some point the Android
Framework can:

a) Kill the whole process given to the purpose of the application
which will essentialy erase all the objects from the memory

1
2
3
4
5
6
7
> These processes have no direct impact on the user experience. Provided
> they implement their Activity life-cycle correctly (see Activity for
> more details), the system can kill such processes at any time to
> reclaim memory for one of the three previous processes types. Usually
> there are many of these processes running, so they are kept in an LRU
> list to ensure the process that was most recently seen by the user is
> the last to be killed when running low on memory.

b) Kill ONLY (so essentialy no other object reference will be deleted)
the activities (by finishing them and in essence any fragments as
well) saving their states and creating the Activities Stack and
leaving anything else (Application object, any other static objects,
references that are reachable from any of the roots).

部分如点2.a.的解释

1
2
3
> Usually there are many of these processes running, so they are kept in an LRU
> list to ensure the process that was most recently seen by the user is
> the last to be killed when running low on memory.

来源 - developer.android.com


应用程序不存在典型的生命周期。应用程序对象存在于内存中,直到它本身没有被Android本身或用户手动杀死。

对于以上几点,以下是您的答案:

1)这是事实。

2)a)这也是事实。
2)b)当应用程序进入后台时,您只能保存前台当前活动的数据。此外,当您自行终止应用程序时(通过从最近的列表中删除它),应用程序堆栈中的所有活动及其保存的数据(非持久数据)都被杀死,因为应用程序是所有活动的基础。


是的,你对2b)大多是正确的。

If an activity is paused or stopped, the system can drop the activity
from memory by either asking it to finish, or simply killing its
process.

但是有些情况下没有调用onSaveInstantSate:

Note that it is important to save persistent data in onPause() instead
of onSaveInstanceState(Bundle) because the latter is not part of the
lifecycle callbacks, so will not be called in every situation as
described in its documentation.

Android文档来源

您可以通过启用以下开发人员选项来请求android始终在后台销毁活动。如果您正在调试应用程序,您应该能够逐步完成生命周期方法,看看发生了什么。

设置 - >开发人员选项 - >应用程序 - >不要保留活动


如果您正在寻找官方确认,那么最好再问Google吧:)。

但是我觉得在读完这篇文章后你会更好地理解这些概念。

Android内存管理

android进程生命周期

第一个问题的答案:
是的确认使用DDMS。

答案为2a问题:是的OS可以在任何时间点杀死进程,当需要其他进程的内存时,将导致杀死与进程相关的所有对象。

回答2b问题:

从官方文档

进程生命周期3.后台活动(用户不可见且已暂停的活动)不再重要,因此系统可以安全地终止其进程以回收其他前台或可见进程的内存。如果需要杀死它的进程,当用户导航回活动(再次在屏幕上显示)时,将使用之前在onSaveInstanceState(Bundle)中提供的savedInstanceState调用其onCreate(Bundle)方法,以便它可以在用户上次离开的状态下重启自己。


好的,在我最近几周的搜索任务中,我能够获得更多信息,现在我可以回答我自己(并希望其他人)的问题:

1)正确

2a)正确

2b)错。 android框架,如果需要内存或者由于某些其他原因它必须"杀死/减少"应用程序,它只能通过杀死专用于该应用程序的整个进程来实现。 android框架可以更直接地杀死所选活动或杀死所有活动,但让所有其他对象保持活动状态(如Application对象,单例等)


那么这件事取决于Android OS的运作方式。 Android Device是一个嵌入式系统,但与PC几乎相同,当我说Android作为操作系统时,它将定义具有操作系统的所有功能。您指出的是Android OS的内存管理和调度功能。
MMU(存储器管理单元)在逻辑上优先考虑当前正在执行的任务,即你的启动器或任何其他应用程序。我想回答的两件事可以帮助你更多:

  • 视图(无论是生成xml还是生成Javacode,它们都是动态生成的。)。
  • Android OS将所有应用程序作为Dalvik虚拟机上的子流程(活动)的流程运行。
  • 在创建活动之前,它们都是空的,当它们被创建时,它们的实例就会生成。再次打开它们时,它们保存的实例再次被查看(Singleton Design Pattern的概念)。
  • 所以,让我告诉你,我不认为这两种选择都是正确的。我相信的是:
    1.视图将始终动态生成。
    2.实例将保存在内存中。
    3.在应用程序的背景上,可用实例的整个过程将是它们的内存。