Unlike other programming modes, activity in Android has no main() function. We cannot determine the activity creation and destruction process, the creation and destruction of activiy (i.e. life cycle) is completed by the system and the system will callback the activity corresponding member function at various stages of the activity's life cycle.
Suppose you want to complete something in the life cycle phase of the activity, all you have to do is rewrite the corresponding life cycle callback function and complete the corresponding task in the function. Activity's life cycle functions are OnCreate (), onstarted (), onresumed (), onpaused (), OnStop (), OnDestroy (), Onrestart ().
The following is the life cycle Pyramid of the Activiy. When we started the app for the first time. The acivity instance specified in Androidmanifest.xml android.intent.action.MAIN is created. The activity instance's onCreate() , onStart() and onResume() life-cycle methods are called sequentially. Activity enters the resumed state. Activity in the resumed state is currently active on the screen today. When you press the home key of the phone. The activity onPause() and onStopped() life cycle functions are called sequentially. Activity enters the stopped state, assuming that the app,activity onRestart() and onStart() life cycle functions are called again, assuming that in stoppped state, the app is actively shut down or the system is shut down due to insufficient resources. The activity's onDestroy() life cycle function is called.
Figure 1 Activity life cycle pyramid
The following combination of code to see the above process:
PackageCom.example.TestActiviyLifecycle;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.util.Log; Public class myactivity extends Activity { Private Static FinalString TAG ="Com.example.TestActiviyLifecycle";/** * Called when the activity is first created. */ @Override Public void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.main); LOG.D (TAG,"OnCreate () called"); }@Override protected void OnStart() {Super. OnStart (); LOG.D (TAG,"OnStart () called"); }@Override protected void Onrestart() {Super. Onrestart (); LOG.D (TAG,"Onrestart () called"); }@Override protected void Onresume() {Super. Onresume (); LOG.D (TAG,"Onresume () called"); }@Override protected void OnPause() {Super. OnPause (); LOG.D (TAG,"OnPause () called"); }@Override protected void OnStop() {Super. OnStop (); LOG.D (TAG,"OnStop () called"); }@Override protected void OnDestroy() {Super. OnDestroy (); LOG.D (TAG,"OnDestroy () called"); }}
Launch app
Logcat Output log:
Press the home key before opening the app
After pressing the home key. Logcat Output log:
Then go to the app again. Logcat Output log:
Horizontal screen, vertical screen switch
After opening the app in portrait mode (when activity is in resumed state), turn on the vertical screen switch, switch the phone to landscape mode, Logcat output log:
When you switch between horizontal and vertical screens, the activity is destroyed and rebuilt.
Data preservation and recovery
When an activity instance enters the destroyed state from the resumed state, its member function onSaveInstanceState(Bundle) is called, in which you can store the data that needs to be saved in the bundle object. When the activity is subsequently rebuilt, the bundle object is passed to the callback function onCreate(Bundle) and onRestoreInstanceState(Bundle) function, and you can retrieve the data from the bundle object to reconstruct the activity.
Figure 2 Data preservation and recovery in activity
Add a edittext to the above Activiy, such as the following:
<?xml version="1.0" encoding="utf-8"?
><linearlayout xmlns:android="Http://schemas.android.com/apk/res/android" Android:orientation="vertical"android:layout_width="Match_parent"android: Layout_height="Match_parent" > <EditTextandroid:id="@+id/edittext_text"android:layout_width ="Match_parent"android:layout_height="wrap_content"/> </linearlayout>
Package execution App. After you enter text in EditText, then rotate the screen. The contents of the EditText are missing.
In order to solve the above problem, we need to save the data in EditText. and restore the data when the activity is rebuilt, add the following code to the activity above:
@Override protected void Onsaveinstancestate (Bundle outstate) {super . Onsaveinstancestate ( Outstate); TextView TextView = (TextView) Findviewbyid (R.id.edittext_text); Outstate.putstring (Extra_text, Textview.gettext (). toString ()); } @Override protected void onrestoreinstancestate (Bundle savedinstancestate) {
super . Onrestoreinstancestate (savedinstancestate); String Text = savedinstancestate.getstring (extra_text); TextView TextView = (TextView) Findviewbyid (R.id.edittext_text); Textview.settext (text); }
The activity's member function onSaveInstanceState() is called before the activity enters the stopped state, so the data in the text input box is fetched and saved in the function in the bundle (to hold the KEY-VALUE data structure).
onRestoreInstanceState()will be called when the activity is rebuilt. And pass the activity's last data saved in the bundle to the function. The ability to retrieve the last saved data from the input parameters and restore it.
Now after entering content in portrait mode. The contents of the rotated screen edittext still exist.
References:
[1] Google Android Official documents
Specific explanations for the "Android" Activity life cycle