The article reads the Android Activity life cycle

First, the life cycle of AcTIvity under normal conditions:

AcTIvity's life cycle can be roughly divided into three parts

The entire life cycle: onCreate() can set all the "global" states, onDestory() can release all resources

Visible life cycle: onStart () shows the user's UI data and resources, onStop () can release resources when it no longer needs to be displayed

The life cycle of the foreground: this life cycle is active, onResume() returns to the foreground, onPause() is invisible or is overwritten

Do not perform time-consuming operations in the onCreate() method. OnStart() and onResume() must not perform excessive operations. Otherwise, opening the desktop application icon will stall.


(1) onCreate: indicates that AcTIvity is being created. You can call setContentView to load layout resources, initialize controls, set listeners, and initialize some global variables. Because in the life cycle of AcTIvity, onCreate method will only be executed once. These controls, listeners, and global variables are also not lost when restoring or restarting under Paused and Stopped states. Even if the memory is insufficient, it is recycled, and if it is Recreate again, it will be the beginning of a new life cycle and the onCreate method will be executed again.

(2) onRestart: Indicates that the activity is restarting. This method is triggered when an activity that is not in the top state needs to return to the top of the stack again to be presented to the user. The scenes that are invoked are: after the home button, and then switch back, after the Activity jumps to another Activity, press the back key to return to the original Activity. It is rarely called because it may only be called after onStop, so it is better to place the operation in the onStart method.

(3) onStart: Indicates that the Activity is starting. At this point, the Activity is visible, but it has not yet appeared in the foreground and cannot interact with the user.

(4) onResume: Indicates that the Activity is visible and appears in the foreground and starts activities.

(5) onPause: Indicates that the Activity is stopping. Under normal circumstances, onStop will be called, but the transparent Activity is opened, onStop will not be called, and onResume will be called. Can perform some light release work (time-consuming will affect the new Activity display, onPause must be executed, the new Activity's life cycle will be executed) For example: stop the animation, cancel the broadcast receivers and so on.

(6) onStop: Indicates that the activity is about to be stopped. It is highly likely to be recycled by the system. In some extreme cases, the system may directly kill the application process instead of calling the onDestory method, so we need to do it in the onStop method. Possible release of resources that users do not need to use temporarily to prevent memory leaks. Can do some light weight recovery work, such as: data storage, the need for database operations.

(7) onDestory: said Activity will be destroyed to determine whether some resources have not been released, do some final clean-up work, such as a thread opened in the Activity's onCreate, then you must determine in the onDestory whether it ends If it does not, end it.

Several life cycle processes:

(1) There are now two activities, A and B, that all overwrite all life cycle methods.

Go to A and callback: AonCreate-->AonStart-->AonResume. Back to the desktop: AAonPause-->AonStop.

Click button to enter B, callback: AonPause-->BonCreate-->BonStart-->BonResume-->AonStop.

Press the back key and callback: BonPause-->AonRestart-->AonStart-->AonResume-->BonStop-->BonDestory

(2) start the dialog style Activity, there are two Activity, A and B (dialogous Activity).

From A to B, callback: AonPause-->BonCreate-->BonStart-->BonResume does not call onStop.

Press the back key, BonPause-->AonRestart-->AonStart-->AonResume-->BonStop-->BonDtopory callback: BonPause-->AonResume-->BonStop-->BonDestory.

The startup dialog does not have a life cycle change. Here is the activity of the transparent theme.

Second, on SaveStateInstance

When the system configuration changes or the memory bottleneck occurs, an exception occurs in the life cycle, and the two brothers onSaveInstanceState and onRestoreInstanceState are called, which solves the memory problem of the Android application.

When rotating the screen, the life cycle is probably as shown below (if you don't want to, you can set android:configChanges="orientation|keyboardHidden|locale" in the manifest file):

onSaveInstanceState: Before the Activity (before onStop) is reclaimed, the system will call onSaveInstanceState(Bundle outState) to save the state of the View and pass it to the passed outState object.

onRestoreInstanceState: When the Activity is recreated, the saved state information is passed in onCreate(Bundle savedInstanceState) and onRestoreInstanceState(Bundle savedInstanceState) and the state of the View is restored. (OnCreate mainly rebuilds Fragment, onRestoreInstanceState restores Window state)

The state of the View control is saved to meet two conditions:

1, View has a unique ID;
2, the initialization of View to call setSaveEnabled(true)

Three points of attention to restore the View control:

1, if the id is repeated, the status of the View will be overwritten

2, the saved Fragment will be automatically created in the onCreate and added to the FragmentActivity

3, the saved View will not be automatically created, just get savedInstance for update View by id

CM Nozzle

Cm Nozzle,Panasonic Cm Nozzle,Smt Cm Nozzle,Panasonic Smt Cm Nozzle

Shenzhen Keith Electronic Equipment Co., Ltd. , https://www.aismtks.com