Discussion on the life cycle of activity in Android

1. Complete life cycle

The above figure is the life cycle diagram of Android activity. The states of resumed, paused and stopped are static. The activities in these three states exist for a long time.

(1) Resumed: in this state, the user can interact with the activity. The activity is at the front end (2) paused: in this state, the activity is covered by another activity, which cannot accept user input information. Another activity comes to the front, translucent, but does not cover the entire screen. (3) Stopped: in this state, the activity is completely hidden and invisible. Keep the current information and the activity does not execute any code. (4) Created and started: after the system calls oncreate(), it quickly calls onstart(), and then quickly executes onresume().

The above is the entire life cycle of Android activity.

2. Primary activity

The user can specify the main interface of the program startup. At this time, the oncreate() method declared as "launcher or main" activity is called to become the entry function of the program. The entry activity can define the main activity in androidmanifest.xml. At this point, the main activity must be declared with the following label:

3. A new activity instance

The system first calls the oncreate () method of the new activity, so we must implement the oncreate () method. Such as declaring UI elements, defining member variables, configuring UI, etc. However, there should not be too many things to avoid starting the program for too long and not seeing the interface.

Once oncreate() is executed, the onstart() and onresume() methods will be called. The activity will not stay in the created or started state.

4. Destroy activity

The last callback of the activity is ondestroy (). The system will execute this method as the signal that your activity will completely delete from the system. Most apps do not need to implement this method, because the references of local classes will be destroyed with the destruction of the activity. And the activity should perform the operation of the activity resource in the onpause () and onstop () methods. If the activity creates background threads during oncreate() or other resources that may cause memory leaks, you should kill them during ondestroy().

The system usually calls onDestroy () after executing onPause () and onStop (), unless finish () is invoked in onCreate (). For example, if your Activity just performs a temporary logical jump function, it is used to decide to jump to the next Activity, so you need to call finish () in onCreate (). The system will directly call the ondestroy method, and other life cycles will not be executed.

5. Pause and resume

The current activity is blocked by other visible components, the current activity is partially visible, and the current activity enters the pause state. The system calls the onpause() method in the activity and executes the onresume() method to recover.

If the current activity is completely blocked by other components and the current activity is completely invisible, the current activity enters the stop state.

When the system calls onpause () in your activity, technically speaking, it means that your activity is still in a partially visible state. Usually do the following in the onpause () callback method.

(1) Stop animation or other running operations to reduce CPU waste (2) submit unsaved changes, but only the contents saved when the user leaves, such as e-mail (3) release system resources, such as broadcast receivers, sensors, GPS or any other resources that affect power consumption. (4) If the program is using camera, onpause () is a good place to release resources.

Generally, onpause () should not be used to save the data changed by the user to permanent storage. You can store the data to permanent storage only when you confirm that the user expects those changes to be saved automatically. However, you should avoid performing CPU intensive work when onpause(), such as writing data to DB, because it will cause the switching activity to become slow. This work should be placed in onstop ().

If the activity is actually to be stopped, you should reduce the workload in onpause and improve fluency.

Resume activity

When the user recovers from the pause state, the onresume() method is called. At this time, the activity is in the foreground, including the first creation. At this time, you should initialize the components you released in the onpause method in onresume, and perform the initialization actions required for each time the activity enters the resumed state.

6. Stop and restart activity

Properly stopping and restarting the activity will make the user aware of the progress of the program. The following scenarios involve stopping and restarting:

(1) The user opens the menu of the recently used app and switches to another app. At this time, your app is stopped. When the user returns to your app, your activity is restarted. (2) The user starts the operation of a new activity in the app. The current activity will stop after the new activity is created. If the user clicks the back button to return to the previous activity, restart (3) when the user uses the app and receives an incoming call.

The stop status UI is not visible. When the activity stops, the system will save the activity instance in memory. Sometimes there is no need to use onstop(), onrestart(), or even onstart() methods in advance. Because most activities are relatively simple, they will stop and restart themselves. You just need to use onpause to stop the running action and disconnect the system resource link.

What is shown above is that when the user leaves your Activity, the system calls onStop () to stop Activity. When the user returns, onRestart () is called, then onStart () and onResume (onStart) are invoked quickly. Whatever the reason is, Activity stops. The system always uses onPause before onStop.

Stop activity

When your activity calls the onstop method, the activity is no longer visible, and all resources that are no longer needed should be released. Once your activity stops, the system will destroy its instance when it is not needed. In extreme cases, the system will directly kill your app process and will not execute the ondestroy() callback function of activity, so you need to release resources in onstop(), otherwise memory leaks. Although the onPause method is called before onStop, you should use onStop to perform CPU-intensive shut-down operations. For example, write data to DB.

When the activity stops, its objects will be saved in memory and called again when resuming. There is no need to initialize those components saved in memory before resuming to the resumed state. The system saves the current state of each view in the layout for us. Even if the system destroys the activity when the activity stops, it will still save the state of the view object into a bundle and restore them when the user returns the activity.

Recreate activity: when the activity is rotated on the screen, it will be destroyed and recreated. Some alternative resources, such as layout, will be loaded. By default, the system uses the bundle instance to save the information in each view object. In order for the Android system to restore the view state in the activity, each view must have a unique ID

In order to ensure additional data to the saved instance state, there is an added callback function in the declaration cycle of the activity. Onsaveinstancestate() must be rewritten. When the user leaves your activity, the system will call it. When the system calls this function, the system will pass the bundle object when your activity is destroyed. In this way, you can add additional information to the bundle and save it in the system. If the system wants to recreate the activity instance after the activity is destroyed, the previous bundle object will be passed to the onrestoreinstancestate() method and oncreate() method of the activity.

Save activity state: when the activity starts to stop, the system will call onsaveinstancestate(), so your activity can save the state information with the collection of key value pairs. This method will save the state information of the activity view by default, such as the text in the EditText component or the sliding position of the listview. In order to save additional state information for the activity, you must implement onsaveinstancestate() and add key value pairs to the bundle. For example:

Restore activity state: when your activity is rebuilt from destroy, you can restore the saved state from the bundle passed to your activity by the system. Both oncreate() and onrestoreinstancestate() callback methods received the same bundle, which contains the same instance state information. Because the oncreate () method will be called when creating a new activity instance for the first time and when re creating an instance destroyed before, you must check whether the bundle object is null before trying to read it. If it is null, the system will create a new activity for the first time. Otherwise, restore the destroyed activity.

We can choose to implement onrestoreinstancestate () instead of restoring data in the oncreate method. Onrestoreinstancestate() method will be executed after onstart() method. The system will only call onrestoreinstancestate() when there is state information to be recovered. Therefore, it is not necessary to check whether the bundle is null.

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