Activity Lifecycle Summary in Android _android

Source: Internet
Author: User

Overview

There is a picture of the truth, so first figure:

Pictured above is the lifecycle flowchart of the activity from the Android website, which describes all the possible occurrences of the activity during its lifecycle and the sequence in which it occurs, and the following is a detailed description of the activity's lifecycle in conjunction with this diagram.

Four basic states of activity

The activity lifecycle is generally divided into four basic States, namely the active state (running), the paused state (paused), the Stop state (stopped), and the death state.

1. Activity status (running)

An active state generally refers to the activity being displayed in the most prominent position on the screen, that is, the activity is at the top of the Android stack.
Generally, when an activity is created, it is in that state.
The functions and sequence of triggers are: OnCreate ()->onstart ()-> onresume ().
which
OnCreate () is invoked only when the activity is first created, primarily for the general initialization settings of the activity, including the creation of the view, the binding of the data, and so on. Note that if there is a previously frozen state (that is, the system has called the Onsaveinstancestate () function on the activity), it can be restored by its Bundle parameter.
OnStart () is invoked when the activity is becoming visible. In general, a broadcast can be registered and so on.
Onresume () is invoked when the activity will interact with the user, at the top of the active stack.

2. Suspend status (paused)

A paused state generally refers to a state in which the activity has lost focus but is still visible (including partially visible). An activity in a paused state is forced to end only if the system is desperately lacking in memory resources.
The function and order of the run state to the paused state are: Onresume ()-> onpuased ().
The function and order of the paused state revert to run State are: onpuased ()-> onresume ().
which
Onpuased () is called when an activity loses system focus, including a partial occlusion of the meeting, and the device into hibernation and so on. In general, some unsaved data is persisted during this period and other CPU-intensive operations are stopped, while no time-consuming operation is required, otherwise blocking the system UI thread.

3. Stop state (Stopped)

A stop state generally refers to a state in which the activity is completely covered by another activity, which is still in all its state, but because the activity becomes invisible, the system often forces the activity to end because of insufficient memory.
The functions and sequences that are triggered by the paused state to the stop state are: onpuased ()-> onStop ().
The function and order of the Stop state recovery to run State are: OnStop ()-> Onrestart ()-> OnStart ()-> onresume ().
which
OnStop () is invoked when an activity becomes invisible, either because the activity is cancelled or if new activity completely masks the activity. During this time, you can generally cancel a registration broadcast, and so on, because the user is not visible.
Onrestart () is a priority call when an activity resumes from a stop state to a running state.

4. State of death

The state of Death refers to the destruction of the activity by the system. When an activity is paused or stopped, it is possible to go into a state of death, as the system may forcibly end the activity due to insufficient memory.
The stop state to the death state is divided into two situations: (1) The user action causes the execution: OnStop ()-> OnDestroy (). (2) is automatically enforced by the system, the activity is forcibly terminated.
which
OnDestroy () is called when an activity is being invoked by the system finished.

The seven life cycle functions of activity

The activity has seven life cycle functions, namely OnCreate (), Onrestart (), OnStart (), Onreusme (), OnPause (), OnStop (), Ondestory (). Their invocation of the situation and the role has been in the previous paragraph related to the introduction and analysis, this will not repeat.
In fact, in an activity. The seven life cycle functions mentioned above are not necessarily executed throughout the life cycle, and some life cycle functions will be skipped or not executed due to insufficient system memory.
Generally OnCreate (), Onrestart (), OnStart (), Onreusme () These four functions cannot be skipped by the system, and the Ondestory () function is skipped directly by the system when the system is out of memory.
The OnPause () function is special, and the activity is forced to end even when the system is out of memory, because the next activity will not continue until the function is finished. So this function is often used to persist data, while also prohibiting any time-consuming operations.
The OnStop () function is the most special, and the OnStop () function can be skipped by the system when the Android environment is Honeycomb version 3.0. However, after Honeycomb is 3.0, the OnStop () function and the OnPause () function cannot be skipped without execution.


Toggle life cycle events triggered by the screen

1. Do not set the activity of the android:configchanges, the screen will recall each life cycle, cut the horizontal screen will be executed once, cut the vertical screen will be performed two times.
2. When setting the android:configchanges= "orientation" of an activity, the screen will also recall each lifecycle, cut horizontally, and the vertical screen will only be executed once.
3. Before Android3.2, when setting the android:configchanges= "Orientation|keyboardhidden" of an activity, the cut screen does not recall each lifecycle. Only the Onconfigurationchanged method is executed. However, after Android3.2 and then, the lifecycle is still invoked again, because screen size also starts to change as the device switches on and off.
4.android3.2 and thereafter, the android:configchanges= "orientation|keyboardhidden|screensize" of the activity is set, and the cut screen does not recall each lifecycle. Only the Onconfigurationchanged method is executed.

Summarize

About one of the four components of Android the lifecycle profile of an activity is written here, but there are many aspects that are not covered, such as the Onsaveinstancestate () function and the onrestoreinstancestate () The use of functions and the difference between the OnPause () function is not involved, the next opportunity to make a good summary, it is really trapped dead, and finally, if this article is negligent and improper place welcome everyone.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.