2

I want to discuss the Android life cycle. I know the basics of how the Android life cycle works but there are some questions I want to ask.

  1. Why don't Android apps move from running to stop directly why it first goes through the pause phase ?

  2. Why don't Android apps move from Running to Destroy directly like IPhone apps? Why does it move from Pause->Stopped->Destroy why not Running->Destroy ?

4

1 回答 1

2

Apps are not immediately destroyed mainly for performance reasons. From the Android Developer Activity Reference:

The Android system attempts to keep application process around for as long as possible, but eventually will need to remove old processes when memory runs low.

If the screen is still powered on and there's no pressing need for your device to free up memory, for example, it's obviously faster for the user if the activity is simply paused instead of completely destroyed. Multiple end lifecycle stages make this possible.

The various callbacks indicate different things. This allows you to separate your cleanup functions into the quick, critical items (disconnecting from system resources like a database or network connection) vs. the longer term tasks (persisting user data across launches of your application, for instance). Because your activity is kept around as long as possible, you can avoid the expensive operations when the user is just pausing the activity momentarily.

onPause() is the first callback, indicating your activity is no longer in the foreground. An app will be paused if a dialog appears or if the user pushes the Home button, for instance. If the app is paused for a dialog, for example, you wouldn't want it to be destroyed because it's still visible.

If you also receive onStop(), it indicates your activity is no longer visible. At this point the user no longer sees what you are displaying. This could mean the user opened another application, for example. Even at this point, however, the system may still keep your activity around to make it quicker for the user to return to it later. (If you/the user ended the activity, however, then it will not be kept and will proceed with the end lifecycle callbacks.)

onDestroy() is the final callback before the activity is destroyed. Note that in extreme cases, the system may destroy your activity without calling this method.

于 2012-09-19T04:38:06.943 回答