Home > Common Problem > body text

Briefly describe the life cycle of activity

angryTom
Release: 2019-07-23 11:52:18
Original
26723 people have browsed it

Briefly describe the life cycle of activity

Recommended tutorial: Android tutorial

This article briefly introduces the Activity life cycle , the content mainly comes from official documents.

Before briefly describing each life cycle method, let’s give an overall overview. We can monitor the Activity life cycle from three levels

Complete life cycle: The complete life cycle begins with the onCreate method callback and ends with the onDestroy method callback
Visible cycle: The visible cycle begins It starts from the onStart method callback and ends at the onStop method callback
Foreground cycle: The foreground cycle starts from the onResume method callback and ends at the onPause method callback

The following is a brief introduction Let’s look at each life cycle method:

onCreate The first method of the life cycle means that the Activity is being created (started). Special note: If you call the finish method within this method, the onDestroy callback will be triggered immediately, and other life cycles will not execute

onRestart The premise for this method to be triggered: the onStop method is called. The onStop method is called when the Activity is invisible and becomes visible again. After this method is called back, the system will trigger the onStart and onResume methods one after another.

onStart Called when the Activity is visible (the Activity is not in the foreground at this time): Called after the onCreate method or when the Activity is invisible due to the onStop method being called, the Activity is visible again

onResume The callback of this method identifies that the Activity is in the foreground. The official documentation indicates that this is more suitable for animation startup and exclusive device access (such as cameras).

OnPause This method is called when Activity is about to enter the background. It is important to note that if Activity A starts Activity B, Activity B will not be created until Activity A's onPause method callback is completed, so do not perform time-consuming operations in this callback method.

onStop This method is called when the Activity changes from visible to invisible

onDestroy The last method of the life cycle, indicating that the Activity is about to be destroyed. The official documentation states that in some cases, the system will simply and rudely kill the Activity host process (as marked 1 in the diagram below), so we should not rely on this method for data storage, but can use this method for resources. Release operation

The following picture is the diagram given in the official document

Briefly describe the life cycle of activity

Activity life cycle diagram

This picture is very clear and rigorous, but the official The description of each life cycle callback method in the document is not very detailed, so if you do not read the document carefully, it will be difficult to understand the direction of the life cycle methods in the picture, and you may misunderstand some methods. For example, some children may misunderstand the onStop method and mistakenly believe that the method is called when the Activity enters the background. In fact, the onPause method is called when entering the background, and the onStop method is called when it is invisible; some children mistakenly believe that the onResume method is called back. Interaction is possible. In fact, we should determine whether interaction is possible based on the hasFocus parameter in the public void onWindowFocusChanged (boolean hasFocus) callback.

Whether the Activity is visible or not and whether it is in the foreground are two different things, onStart is paired with onStop to describe whether the Activity is visible; onPause is paired with
onResume to describe whether the Activity is in the foreground. Visibility of an Activity does not mean that it can be interacted with, and similarly, it may not be able to be interacted with if it is in the foreground. A direct example is that if the Activity displays a Dialog, the Activity is still in the foreground at this time, but we cannot interact with the Activity

In addition, I believe many people have not personally verified the direction marked 2 in the above diagram. Yes, because it's often difficult to recreate this scenario interactively. For this scenario, we can simulate it through code control: Activity A starts Activity B, and calls the finish method directly in the onCreate method of Activity B

The above is the detailed content of Briefly describe the life cycle of activity. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template