Activity lifecycle summary in Android

summary

There is a picture and there is a truth, so the picture above first:

The above figure is the life cycle flow chart of an activity intercepted from the official Android website. The structure is very clear. It describes all possible situations and the order of occurrence of an activity in its life cycle. The following will introduce the life cycle of an activity in detail in combination with this figure.

Four basic states of activity

The activity life cycle is generally divided into four basic states: running, paused, stopped and dead.

1. Active status (running)

Activity status generally means that the activity is displayed in the most prominent position on the screen, that is, the activity is at the top of the Android activity stack. Generally, an activity is in this state after it is created. The functions and sequence triggered during are oncreate() - > onstart() - > onresume(). Oncreate() is called only when the activity is created for the first time. It is mainly responsible for the general initialization settings of the activity, including view creation, data binding, etc. It should be noted that if there is a frozen state before (that is, the system has called onsaveinstancestate() function on the activity), the state can be restored through its bundle parameter. Onstart() is called only when the activity is becoming visible. Generally, a broadcast can be registered during this period, and so on. Onresume() is called when the activity is about to interact with the user. At this time, the activity is at the top of the activity stack.

2. Suspended status

Pause status generally refers to the status that the activity has lost focus but is still visible (including partially visible). A suspended activity will be forced to end by the system only when the system is extremely short of memory resources. The functions triggered from running state to suspended state and their sequence are: onresume() - > onpuased(). The functions and sequence triggered by resuming the suspended state to the running state are: onpuased() - > onresume(). Among them: onpuased() is called when an activity loses the system focus, including the meeting is partially blocked, and the device goes to sleep, etc. Generally, during this period, some unsaved data is persisted and other CPU consuming operations are stopped. At the same time, time-consuming operations are not allowed, otherwise the system UI thread will be blocked.

3. Stopped

The stop state generally refers to the state in which the activity is completely covered by another activity, which means that it still maintains all States. However, since the activity becomes invisible, the system often forcibly ends the activity due to insufficient memory. The functions and sequence triggered from pause state to stop state are: onpuased() - > onstop(). The functions and sequence triggered when the stop state returns to the running state are: onstop() - > onrestart() - > onstart() - > onresume(). Where: onstop() is called when an activity becomes invisible. At this time, it may be because the activity is to be logged off or the new activity completely obscures the activity. During this period, you can generally cancel the registration broadcast and other operations, because the user is not visible. Onrestart () is called preferentially when an activity recovers from the stopped state to the running state.

4. State of death

Dead status means that the activity is destroyed by the system. When an activity is in a suspended or stopped state, it may enter a dead state everywhere, because the system may forcibly end the activity due to insufficient memory. There are two situations from stop state to death state: (1) if it is caused by user operation, execute: onstop() - > ondestroy(). (2) If it is automatically enforced by the system, the activity is forcibly terminated. Where: ondestroy() is called when an activity is being finished by the system.

Seven life cycle functions of activity

Activity has seven lifecycle functions, oncreate(), onrestart(), onstart(), onreuse(), onpause(), onstop(), ondestory(). Their invocation and functions have been introduced and analyzed in the previous paragraph, and will not be repeated here. In fact, in the whole life cycle of an activity, the above seven life cycle functions are not necessarily executed. Sometimes, due to insufficient system memory, some life cycle functions will be skipped and not executed. Generally, the four functions oncreate(), onrestart(), onstart(), onreusme() cannot be skipped and not executed by the system. When the system memory is insufficient, the ondestory() function will be directly skipped and not executed by the system. The onpause() function is special. Even when the system memory is insufficient, the activity will not be forced to end until the function is executed. The reason is that the next activity will not continue until the function is completed. Therefore, this function is often used to persist data, and any time-consuming operation is prohibited. The onstop() function is the most special. When the Android running environment is Honeywell, that is, before 3.0, the onstop() function can be skipped by the system and not executed. However, in the version of Honeywell (i.e. after 3.0), the onstop() function, like the onpause() function, cannot be skipped and not executed.

Toggles the lifecycle events triggered by the vertical and horizontal screens

1. When Android: configchanges of activity is not set, screen cutting will call each life cycle again. It will be executed once when cutting horizontal screen and twice when cutting vertical screen. 2. When setting Android: configchanges = "orientation" of activity, screen cutting will still call each life cycle again, and it will only be executed once when cutting horizontal and vertical screens. 3. Before Android 3.2, when setting Android: configchanges = "orientation|keyboardhidden" of activity, screen cutting will not call each life cycle again, but only execute onconfigurationchanged method. However, in Android 3.2 and beyond, each life cycle will still be called again, because the screen size also begins to change with the horizontal and vertical switching of the device. 4. Android 3.2 and later, set Android: configchanges = "orientation|keyboardhidden|screensize" of activity. Screen cutting will not call each life cycle again, but only execute onconfigurationchanged method.

summary

The brief introduction to the life cycle of activity, one of the four major components in Android, is written here. In fact, there are many aspects that have not been introduced. For example, the usage of onsaveinstancestate() function and onrestoreinstancestate() function and the difference between onsaveinstancestate() function and onpause() function are not involved. I will summarize it next time. It's really sleepy, Finally, if there is any negligence and misconduct in this article, you are welcome to correct it.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>