views:

59

answers:

3

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

A: 

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.

EboMike
I'm asking about the Application class (http://developer.android.com/reference/android/app/Application.html) not Activities. I am wondering how the Application class works in relation to the life cycles of it's Activities
littleFluffyKitty
and by "Stop" I mean no longer run any processes, and/or exist in memory in a way that the Activities can access it's data.
littleFluffyKitty
Let's put it this way: What are you trying to achieve, or what problem are you trying to solve? What do you need the Application object for?
EboMike
I'm just trying to understand the relationship between the Application and the Activity, I'm not trying to solve a specific problem per se.
littleFluffyKitty
Application is basically persistent across the lifetime of your app. You have the option to override it to store some semi-persistent data in there (persistent across several Activites, but bound to disappear as soon as the user goes away). Activites are individual screens. Their instances may remain in memory but paused, but you should assume that they MAY be deleted whenever you move away from the activity. There's even a debug option in the Android debugging tools to destroy activities immediately.
EboMike
And in real life, you typically don't deal with the Application object. I never have, at least.
EboMike
A: 

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.

hackbod
+1  A: 
  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

satur9nine