Android 的 Activity 生命周期如何与整个应用程序相关?
这似乎没有得到很好的记录,或者我错过了它,所以在我运行一堆自己的测试之前,我想知道是否有人已经知道其中一些问题的答案。
首先,当我说“应用程序”时,我指的是扩展 Application
类。 http://developer.android.com/reference/android/app/Application。 html
我的问题如下,有些是相关的。
当用户从应用程序中离开活动并转到另一个应用程序的活动时,即使应用程序没有 onPause(),该应用程序是否也会以某种方式暂停?或者它是否会继续保持未暂停状态,直到其所有活动都被销毁?
应用程序何时停止?当它的所有 Activity 都被销毁时?
应用程序活动之一是否有可能在没有应用程序实例的情况下运行,或者如果活动之一存在,应用程序类将始终存在吗?
如果应用程序上正在运行某个进程,并且它的 Activity 都已暂停,该进程会继续运行吗?
应用程序是否会以任何方式受到轮换的影响,或者轮换是否仅更改活动?
谢谢
This doesn't appear to be well documented or I missed it, so before I run a bunch of my own tests I was wondering if anyone already knows the answers to some of these questions.
First off, when I say "Application" I am referring to extending the Application
class. http://developer.android.com/reference/android/app/Application.html
The questions I have are as follows, some are related.
When an a user leaves an Activity from within the Application, and goes to the Activity of another application, does the Application somehow get paused as well, even though it doesn't have an onPause()? Or does it continue to live unpaused until all of it's activities are destroyed?
when does the Application stop? When all of it's Activities are destroyed?
Is there ever a chance that one of the Applications Activities could be running without an instance of the Application, or will the Application class always exist if one of the Activities does?
If there is some process running on the Application, and it's Activities are all paused, will that process continue to run?
Is the Application effected by rotation in any way or does rotation only change Activities?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
正如您所说,应用程序没有 onPause,因此应用程序没有任何反应。当您的 Activity 中调用 onPause 时,不会发生任何特殊情况,您的 Activity 会继续运行并可以做任何它想做的事情,包括运行新线程、计时器可以关闭等等。
我相信您要问的是:应用程序何时被销毁以及应用程序中的 onTerminate 方法何时被调用?答案很难确定,并且取决于系统,当所有活动都被 onDestroyed 调用时,它不一定会发生。事实上,即使调用 onDestroy,您的 Activity 也不一定会被垃圾收集。当系统内存不足时,您的应用程序所在的进程可能会被杀死,这意味着您的应用程序将消失; onTerminate 可能会被调用,也可能不会被调用。那时所有的Activity、Services等也都被杀死了。
Application 总是首先实例化,一个 Activity 必须有一个关联的 Application,就像你在 AndroidManifest.xml 中定义它一样。
Android 中的进程永远不会暂停,onPause 方法除了告诉您暂停应用程序中的操作之外,实际上并不执行任何操作。除此之外,进程不断地嘎嘎作响,您的线程继续运行,甚至主线程也通过 BroadcastReceiver 接收 Intents。
应用程序在应用程序的 onConfigurationChanged() 中获取轮换回调。我不确定您是否可以禁用它,因为 AndroidManifest.xml 中的应用程序标记不支持 configChanges 属性。
与应用程序的一个很好的比较是任何类中的静态字段。只要进程不被破坏,静态字段就会存在,就像应用程序一样。静态字段可以被所有活动、服务等访问(假设静态字段是公共的),就像您的应用程序一样。
祝你好运!
雅各布
As you say the application does not have onPause so nothing happens to the application. When onPause gets called in your Activity nothing special happens, your Activity continues to run and can do whatever it wants including run new threads, timers can go off, whatever.
I believe what you are asking is: when is an Application destroyed and when the onTerminate method in an Application called? The answer is hard to pinpoint and is up to the system, it does not necessarily happen when all activities get onDestroyed called. In fact even when onDestroy is called, your Activities aren't necessarily garbage collected. When the system gets low on memory the process that your Application lives in can be killed, meaning your Application will disappear; onTerminate may or may not be called. At that time all the Activities, Services, etc, are killed too.
The Application is always instantiated first, an Activity must have an associated Application, just like how you define it in the AndroidManifest.xml.
Processes never pause in Android, the onPause method does not actually really do anything other than tell you to pause things in your app. Other than that the process keeps chugging away, your threads keep running, even the main thread receive Intents with a BroadcastReceiver.
The Application gets rotation callbacks in the Application's onConfigurationChanged(). I'm not sure if you can disable that since there is no configChanges attributes supported by application tags in the AndroidManifest.xml.
A good comparison to Application is static field in any of your classes. The static fields will live as long the process is not destroyed, just like the Application. Static fields can be accessed by all Activities, Services, etc (assume the static fields are public), just like your Application.
Good Luck!
Jacob
理解这一点的最简单方法就是忘记应用程序的存在。应用程序与应用程序生命周期无关。它只是一个进程的全局,对于某些事情可能有用,但对于任何事情都不需要。关于应用程序如何运行的一切都围绕着其 .apk 中声明的 Activity、BroadcastReceiver、Service 和 ContentProvider 组件。
The easiest way to understand this is to just forget that Application exists. Application has nothing to do with the application lifecycle. It is just a global on a process, that can be useful for some things, but is not needed for anything. Everything about how an application runs revolves around the Activity, BroadcastReceiver, Service, and ContentProvider components declared in its .apk.
在您的最后一个 Activity 被销毁后,Application 的实例可以继续存在。即使所有活动都消失了(即所有活动都调用了 onDestroy 方法),应用程序实例仍然可能存在。
该应用程序实例可以“重复使用”,否则您可能会认为这是应用程序的两次单独运行。
An instance of Application can continue to exist after your last Activity is destroyed. Even if ALL activities are gone (ie. have all had their onDestroy methods called), the Application instance could still exist.
This application instance could be "re-used" for what you might otherwise think are two separate runs of your application.
这一切都在这里详细解释: http://developer.android.com/reference /android/app/Activity.html。如果你读完它,你应该明白一切。
非常快:
每个活动都有一个 onPause。您可以选择不覆盖它,但它仍然会被调用。一旦你切换离开,onPause 就会被调用。
定义“停止”。定义“应用程序”。该进程可能会永远徘徊,但它只会休眠并等待其活动之一启动。
活动不被实例化就不可能存在。
执行的每一段代码都在一个进程中运行,因此您的应用程序始终有一个进程。当您切换到其他应用程序后,该进程将继续存在,但会处于睡眠状态。如果系统资源不足,Android 可以随时终止该进程。
每次旋转屏幕时,您的 Activity 都会被销毁并重新创建,除非您专门禁用它。
This is all explained in detail here: http://developer.android.com/reference/android/app/Activity.html. If you read through it, you should understand everything.
Real quick:
Every activity has an onPause. You can choose not to override it, but it'll get called nonetheless. As soon as you switch away, onPause will be called.
Define "stop". Define "Application". The process may linger around forever, but it'll simply sleep and wait until one of its activities is started.
It's impossible for an activity to exist without being instantiated.
Every code executed runs in a process, so there's always one process for your app. The process will continue to exist after you switch to a different app, but it'll be in sleeping state. Android could at any time kill the process if system resources run low.
Every time you rotate the screen, your activity will be destroyed and recreated, unless you specifically disable that.