Getting started with Android (III) activity life cycle and startup mode

Original link: http://www.orlion.ga/432/

1、 Activity lifecycle

1. Return stack

Activities in Android can overlap. Every time we start a new activity, it will overwrite the original activity, and then click the back key to destroy the top activity, and the next activity will be displayed again. Android uses tasks to manage activities. A task is a collection of activities placed on the stack, and the stack is also called return stack. By default, we start a new activity, which will be put on the return stack and at the top of the stack. When we press the return key or call finish

When the () method destroys an activity, the activity at the top of the stack will be out of the stack, and the previous activity in the stack will be at the top of the stack again. The system always shows the activity at the top of the stack to the user

2. Active state

Each activity can have up to four states in its lifecycle

(1) 2. Operation status

An activity is running when it is at the top of the stack.

(2) . pause status

When an activity is no longer at the top of the stack but is still visible, it is suspended. You may think that since the activity is no longer at the top of the stack, how can it be visible? This is because not every activity will occupy the whole screen. For example, activities in the form of dialog box will only occupy part of the middle area of the screen. The suspended activity is still completely alive, and the system is not willing to recycle it.

(3) . stop status

When an activity is no longer at the top of the stack and is completely invisible, it enters the stop state.. The system will still save the corresponding state and member variables for this activity, but this is not completely reliable. When memory is needed elsewhere, activities in the stopped state may be recycled by the system.

(4) 1. Destruction status

When an activity is removed from the return stack, it becomes destroyed. The system will most tend to recycle activities in this state, so as to ensure that the phone has enough memory.

3. Activity lifetime

Seven callback methods are defined in the activity class, covering every link of the activity life cycle

(1)onCreate()

This method will be called when the activity is created for the first time. You should complete the initialization of the activity in this method, such as loading layout, binding events, etc.

(2)onStart()

This method is called when the activity changes from invisible to visible

(3)onResume()

This method is called when the activity is ready to interact with the user. At this time, the activity must be at the top of the return stack and in the active state.

(4)onPause()

This method is called when the system is ready to start or reply to another activity. We usually release some CPU consuming resources and save some key data in this method, but the execution speed of this method must be fast, otherwise it will affect the use of the new stack top.

(5)onStop()

This method is called when the activity is completely invisible. The main difference between it and the onpause method is that if the new activity started is a dialog activity, the onpause () method will be executed, while onstop () will not

(6)onDestroy()

This method is called before the activity is destroyed, and the state of the activity will become destroyed.

(7)onRestart()

This method is called before the activity changes from stop state to running state, that is, the activity is restarted. In the above seven methods

Everything except onrestart () is relative, so the activity can be divided into three survival periods

(1) . complete survival

What an activity goes through between the oncreate () method and the ondestory () method is the full lifetime.

(2) . visible survival

What an activity goes through between the OnStart () method and the onstop () method is the visible lifetime. During the visible lifetime, activities are always visible to users, and users may not be able to interact with them. Through these two methods, we can reasonably manage the resources visible to users. For example, resources are loaded in the OnStart () method and released in the onstop () method, so as to ensure that activities in the stopped state do not occupy too much memory

(3) . foreground lifetime

What an activity goes through between onresume () method and onpause () method is the foreground lifetime. During the foreground lifetime, the activity is always running. At this time, the activity can interact with the user.

The whole survival period is shown in the figure below:

@H_ 502_ 83@

4. What if the activity is recycled

When an activity enters the stop state, it may be recycled by the system. If there is an activity a in the application, the user starts activity B based on activity a, and activity a enters the stop state. At this time, due to insufficient system memory, activity a is recycled, and then the user presses the return key to return to activity A. what happens? In fact, activity a will be displayed normally, but instead of executing onrestart() method, it will execute oncreate() method of a, because activity a will be recreated in this case.

But! There may be temporary data and status in activity A. for example, there is a text input box in mainactivity. Now you enter a text, and then start normalactivity. At this time, mainactivity is recycled due to insufficient system memory. After a while, click the return key to return to mainactivity. You will find that the text you just entered is gone because mainactivity is recreated. This is very detrimental to the user experience. A onSaveInstanceState () callback method is provided in Activity. This method will ensure that it must be called before the activity is recovered, so we can solve the problem that the temporary data can not be saved when the activity is recovered.

Onsaveinstancestate() method will carry a bundle type parameter. Bundle provides a series of methods to save data. For example, putstring() method can be used to save string and putint() method can be used to save integer numbers... Each saving method needs to pass in two parameters. The first parameter is a key, which is used to take values from bundle, The second parameter is the data to be saved.

Add the following code in mainactivity to save temporary data:

        @Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		String tempData = "This is temp data";
		outState.putString("tempData" , tempData);
	}

The data has been saved. How should I restore it? The oncreate () method we have been using has a parameter of type bundle. In general, this parameter is null, but if the activity is saved by onsaveinstancestate() method before being recycled by the system, this parameter will have all the previously saved data. We only need to use the corresponding method to get it and modify the oncreate method of mainactivity:

                if (savedInstanceState != null) {
			String tempData = savedInstanceState.getString("tempData");
			Log.d("tag", tempData);
		}

Take out the value and then perform relevant recovery operations.

2、 Active startup mode

There are four startup modes: standard, singletop, singletask and singleinstance. You can select the startup mode in androidmanifest.xml by specifying the Android: launchmode attribute to the < activity > tag.

1、standard

In the default startup mode, Android manages activities by using the return stack. In the standard mode, whenever a new activity is started, it will be stacked in the return stack and at the top of the stack. For activities using the standard mode, the system does not care whether the activity already exists in the return stack. Each startup will create a new instance of the activity.

2、singleTop

When the startup mode of the specified activity is singletop, if it is found that the top of the returned stack is already the activity when starting the activity, it is considered that it can be used directly and no new activity instance will be created.

3、singleTask

When the startup mode of the specified activity is singletask, each time the activity is started, the system will check whether there is an instance of the activity in the return stack. If it already exists, the instance will be used directly, and all activities above the activity will be out of the stack. If it is not found, it will be created.

4、singleInstance

Different from the above three startup modes, the activity specified as singleinstance mode will start a new return stack to manage the activity. So what's the point? Imagine a scenario where an activity in our program is allowed to be called by other programs. If we want to implement other programs and our programs can share an instance of this activity, how should we implement it? It is certainly impossible to use the first three startup modes, because each application will have its own return stack. If the same activity is stacked in different return stacks, it must create a new instance. This problem can be solved by using the singleinstance mode. In this mode, there will be a separate return stack to manage the activity. No matter which application accesses the activity, it will share the same return stack, which solves the problem of sharing activity instances.

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
分享
二维码
< <上一篇
下一篇>>