Android Development notes (3)-Activity Lifecycle
AndroidEclipseGoogleIDE Activity
Note that there are a lot of onxxxx function definitions in the activity API, in addition to the oncreate we used earlier, there are onstart, onstop and onpause. Literally, they are some event callbacks, so what is the order? In fact, this kind of thing cannot be understood by yourself. Before doing this experiment, we must first find out how logs are output in Android.
Obviously, we use the android. util. log class, which is quite easy to use, because it provides some static methods:
Log. v (String tag, String msg); // VERBOSE
Log. d (String tag, String msg); // DEBUG
Log. I (String tag, String msg); // INFO
Log. w (String tag, String msg); // WARN
Log. e (String tag, String msg); // ERROR
The preceding tag is an identifier defined by us. It can be defined by "class name_method name.
If you use Eclipse + ADT to develop the output LOG information, you can see it in LogCat. Otherwise, you can use adb logcat, but I have always relied on the IDE environment.
Now let's modify the preceding HelloThree code:
Public void onStart ()
{
Super. onStart ();
Log. v (TAG, "onStart ");
}
Public void onStop ()
{
Super. onStop ();
Log. v (TAG, "onStop ");
}
Public void onResume ()
{
Super. onResume ();
Log. v (TAG, "onResume ");
}
Public void onRestart ()
{
Super. onRestart ();
Log. v (TAG, "onReStart ");
}
Public void onPause ()
{
Super. onPause ();
Log. v (TAG, "onPause ");
}
Public void onDestroy ()
{
Super. ondestroy ();
Log. V (TAG, "ondestroy ");
}
Public void onfreeze (bundle outstate)
{
Super. onfreeze (outstate );
Log. V (TAG, "onfreeze ");
}
This code is also added to HelloThreeB to compile, run, and analyze the output logs from logcat.
When you start Activity One on the first interface, the order is as follows:
OnCreate (ONE)-onStart (ONE)-onResume (ONE)
Although this is the first time to start, you have to go through this resume event. Then, we click goto to jump to the second Activity Two (the previous one is not closed). The order is as follows:
OnFreeze (ONE)-onPause (ONE)-onCreate (TWO)-onStart (TWO)-onResume (TWO)-onStop (ONE)
Note: before the second Activity Two is started, One will experience a process of freezing and pausing. After Two is started, One will be stopped?
Then, click back to return to the first interface. The order is as follows:
OnPause (TWO)-onActivityResult (ONE)-onStart (ONE)-onRestart (ONE)-onResume (ONE)-onStop (TWO)-onDestroy (TWO)
Note: The Two is directly suspended when it is returned without freezing. After One receives the parameter and restarts, Two stops and is destroyed.
Finally, click Exit to Exit the application. The order is as follows:
OnPause (ONE)-onStop (ONE)-onDestroy (ONE)
If we use finish, there will be no freeze, but it will still be destroyed after pause-stop.
The question is: why is Start Restart the first time? But what is shown on the graph in the document is restart before start? However, the descriptions in the following table seem to be correct, and start is followed by resume (if it is the first time) or restart (if it was stopped, in this case, a restart will be inserted in start and resume ).
Let's take a look at the document.
1. Android uses Activity Stack to manage multiple activities. Therefore, at the same time, only the top Activity is in active or running state. All other activities are under pressure.
2. If the inactive Activity is still visible (that is, if it is pressed with a non-full screen Activity or transparent Activity), it is in the paused state. When the system memory is insufficient, a paused Activity can be killed by the system. I just don't understand what the display on the interface will look like if it is killed? It seems necessary to study this situation next time.
3. The relationship between events can be clearly understood. Create and Destroy are matched with one pair, called entrie lifetime. when resources are allocated during creation, resources are released upon destruction. There are also a pair of Start and Stop at the top, called visible lifetime, this process is visible and non-visible. The top one is the Resume and Pause, called foreground lifetime, which indicates whether the process is in the active state.
4. Therefore, to implement the Activity derived class, we need to reload two important methods: onCreate () for initialization, onPause () for saving the results of the current operation.
In addition to Activity Lifecycle, Android also has a description of Process Lifecycle:
When the memory is insufficient, Android will actively clean up the portal. How does it determine which process can be cleared? The document also mentions its importance sorting:
1. the most vulnerable process is empty process. A blank process is a process that no Activity is bound to it and no application components (such as Services or IntentReceiver) are bound to it, that is to say, there is no activity or service in this process. They are just a cache and can increase the speed when starting a new Activity. They will be cleared first. Therefore, we recommend that you perform background operations in the form of a Service, that is, you should start a Service in the Activity to perform these operations.
2. the next step is the background activity, that is, the process in which the activity is stopped. The invisible activities are cleared, which is indeed safe. The system maintains a LRU list, multiple activities on the background are located here. The system can determine which activities can be cleared Based on the LRU list, and which of them should be cleared first. However, it is mentioned in the document that when the cleared Activity is re-created, its onCreate will be called and the parameter is the Bundle in onFreeze. But what I don't understand here is, Will Android keep this Bundle when the Activity is killed?
3. Then it is the turn of service process, which is a process bound to the Service and started by the startService method. Although they are not visible to users, they are generally being processed for some long-term operations (such as MP3 playback), and the system will protect them unless there is actually no memory available.
4. Then it's the turn of the visible activity, or visible process. As mentioned above, the Paused Activity may also be cleared by the system, but it is relatively safe.
5. the most secure is the foreground activity, which will not be cleared if it is not forced. This process includes not only the activities after resume, but also IntentReceiver instances after onReceiveIntent.
In the discussion of the lifecycle of the Android Application, the document also mentions some precautions: because the survival of the Android Application is not directly controlled by the Application itself, it is managed by the Android system platform. Therefore, for our developers, we need to understand the lives of different components such as Activity, Service, and IntentReceiver. Remember that if the component is improperly selected, it is very likely that the system will kill a process that is doing important work.
The next step is to understand Intent and Its IntentReceiver, and continue the next day.
Note:
After reading this article, I tried it myself, and read the google Documents, http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle
I still read the pictures. I didn't take them seriously in Text E. I didn't see the original version because of the android version, I uploaded the source code of my own test to the attachment. You can run it and try it. I am using version 1.1.
The running image is as follows: