Activity of Android development

back stack

Activity and back stack
1. Running state : at the top of the return stack
2. Paused state : not at the top of the stack but still visible, such as the appearance of a dialog
3. Stopped state : not at the top of the stack and completely invisible, but the system still retains the active member variables and Corresponding state
4. Destroyed state : removed back stack

The life cycle

Activity life cycle
onCreate() : Called when the activity is first created.

onStart() : Called when the activity is made visible from invisible .

onResume : Called when the activity is ready to interact with the user, the activity must be at the top of the stack and running .

onPause : Called when the Activity loses focus but is not completely covered, and the system is ready to start or resume another activity, usually in this method to release some CPU-consuming resources and save some important data.

onStop : Called when the activity is completely invisible. The difference from onPause is that if the new activity is started as a dialog, the activity will only execute the onPause method.

onDestroy : Called before the activity is destroyed.

onRestart : Called before the activity changes from the stopped state to the running state.

Of particular note are:

When jumping from activity1 to another activity2, the onPause method of activity1 will be executed first, and then the onResume method of activity2 will be executed.

The above seven can be summarized as:

Full lifetime: between onCreate() and onDestroy()

Visible lifetime: between onStart() and onStop()

Foreground lifetime: between onResume() and onPause()

To prevent information loss from activities being recycled:

If an activity is in the onPause state, it may be recycled by the system.

But the onSaveInstanceState(Bundle) callback method is called when the system recycles the activity .

This method provides a Bundle type parameter, which can save data through Bundle .

Then the Bundle class in the onCreate method of other activities takes it out.

Lifecycle in Exceptional Conditions

Activity reconstruction process under abnormal conditions
onSavaInstanceState will be called before onStop is called, but it can be called before onPause is called or after onPause is called.
  When the Activity is rebuilt, the system will call onRestoreInstanceState, and onSavaInstanceState will pass the Bundle with the saved data to onCreate and onRestoreInstanceState respectively.
Note that
onSavaInstanceState is only called when the activity is destroyed abnormally.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325586486&siteId=291194637