开发者

How does Android's Activity lifecycles work in relation to the entire Application?

开发者 https://www.devze.com 2023-01-20 21:50 出处:网络
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.

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.

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

  2. when does the Application stop? When all of it's Activities are destroyed?

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

  4. If there is some process running on the Application, and it's Activities are all paused, will that process continue to run?

  5. Is the Application effected by rotation in any way or 开发者_如何学编程does rotation only change Activities?

Thanks


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

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

  3. The Application is always instantiated first, an Activity must have an associated Application, just like how you define it in the AndroidManifest.xml.

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

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


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.


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.


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:

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

  2. Define "stop". Define "Application". The process may linger around forever, but it'll simply sleep and wait until one of its activities is started.

  3. It's impossible for an activity to exist without being instantiated.

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

  5. Every time you rotate the screen, your activity will be destroyed and recreated, unless you specifically disable that.

0

精彩评论

暂无评论...
验证码 换一张
取 消