Detailed explanation of Android activity life cycle

The lifecycle of the activity.

1、 Understand activity

Activity is one of the four major components of Android programs.

Activity is the presentation layer of Android programs. Each display screen of the program is an activity.

Students who have studied web development can understand activity as a JSP file in a web page; Or you can think of it as a Windows window.

Let's take a look at the inheritance relationship of activity class:

From here, we can see that activity is a subclass of context class. Let's have an impression first.

2、 Understanding the life cycle of an activity

The most important and basic function of the mobile phone is to make a call, which means that the current program may be suspended at any time when the call comes, and the existing program may be closed at any time when the power is insufficient; Therefore, Android programs are different from programs on computers. Specifically, the life cycle of an activity is not controlled by itself, but by the Android system.

In Android, activity has four basic states:

1、Running

When it is at the front of the screen, it is visible and interactive with the user.

2、Paused

The state when acitivy is overwritten by another transparent or non full screen activity is called the paused state, which is visible but not interactive. 3、Stop

When an activity is covered by another activity and the interface is not visible, it is in the stop state.

4、Killed

When the activity is killed by the system or not started, it is in the killed state.

Let's take a look at the life cycle of activity:

The following is a simple translation of the above figure:

Activity stack:

Android manages activities through the activity stack.

The running activity is at the top of the stack and is in running state;

When a new activity enters the top of the screen, the original activity will be pressed into the second layer. If its screen is not completely covered, it will be in pause state. If it is covered, it will be in stop state.

Of course, no matter you are at any layer, you may be forcibly closed when the system feels that there are insufficient resources. Of course, the program at the bottom of the stack is closed first.

For example, when you call Activity. in the program, When using the finish () method, the result is the same as when the user presses the back key: he tells the activity manager that the activity instance can be "recycled". Then, the activity manager activates the activity at the second layer of the stack and re enters the stack, pushes the original activity into the second layer of the stack, and changes from running state to paused state.

3、 Example: observe the change of activity status caused by flipping the screen

Some life cycle related methods are defined in the activity class. These methods will be called when the state changes, such as the method oncreate() called during creation. Therefore, we can write a program, write comments in each method of the program, and then look at the printing order of runtime comments to track the change of activity state.

Here is the program code:

Then the LogCat tool is transferred to observe the operation details of the program. Through the logcat filter, we can see the process of program startup, screen flip and program exit by pressing the back key, which helps us verify the knowledge we just learned.

We can see that when the screen is flipped, the Android system kills the activitylife activity first (the specific order is to pause, then close and then destroy), and then starts (the specific order is to create first, then start and then restore). Through this example, we can clearly see that it is the Android system rather than the programmer that controls the life cycle of activity.

The above is an introduction to the Android activity life cycle. Friends in need can refer to 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
分享
二维码
< <上一篇
下一篇>>