Android cainiao study notes 9 ---- Activity (2), cainiao notes
About ActivityLifecycle:
The following are the callback methods during the entire lifecycle of an Activity when the status changes. They correspond to the complete lifecycle of the Activity.
Void onCreate (Bundle savedInstanceState): Callback when Activity is created
Void onStart (): Called after onCreate () or onRestart (), that is, when the Activity is created for the first time or changes from invisible to visible.
Void onResume (): Returns to the active state after onStart. After that, the activity is in the active state and at the top of the stack of the task stack.
Void onPause (): Callback when the focus is lost but is still partially visible.
Void onStop (): Callback when Activity changes to completely invisible
Void onRestart (): Callback when Activity restarts
Void onDestroy (): Callback before Activity is destroyed
Except onRestart (), the above seven methods are displayed in pairs in the graph of the life cycle. It is divided into three pairs, and there are three survival periods.
From onCreate () to onDestroy (), an Activity instance goes through all the processes from creation to destruction.Full survival.
From onStart () to onStop (), an Activity instance changes from visible to invisible.Visible lifetime. Note that it is not necessarily at the top of the stack, so it can interact with users.
From onResume () to onPause (), an Activity instance goes through the process from the Activity state to the pause state. The Activity instance is in the active state and is calledForeground survival, OrActive State Lifetime.
Complete lifecycle program demonstration, refer to the first line of code
There are three activities in the program: MainActivity is the entry, and two buttons are placed to start the other two activities, to implement 7 Life Cycle callback methods, and output a run message respectively; normalActivity is a common Activity; DialogActivity is in Manifest. the theme attribute is configured in xml to make it a dialog box style Activity. <activity android: name = ". dialogActivity "android: theme =" @ android: style/Theme. dialog "> </activity>.
Code:
Main_layout.xml:
1 <?xml version="1.0" encoding="utf-8"?> 2 3 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 4 5 android:layout_width="match_parent" 6 7 android:layout_height="match_parent" 8 9 android:orientation="vertical" >10 11 <Button12 13 android:id="@+id/normal"14 15 android:layout_width="wrap_content"16 17 android:layout_height="wrap_content"18 19 android:text="@string/start_normal_activity"/>20 21 <Button22 23 android:id="@+id/dialog"24 25 android:layout_width="wrap_content"26 27 android:layout_height="wrap_content"28 29 android:text="@string/start_dialog_activity"/>30 31 </LinearLayout>
MainActivity. java:
1 public class MainActivity extends ActionBarActivity implements OnClickListener { 2 3 @Override 4 5 public void onClick(View view) { 6 7 // TODO Auto-generated method stub 8 9 switch (view.getId()) { 10 11 case R.id.normal: 12 13 Intent intent1 = new Intent(this, NormalActivity.class); 14 15 startActivity(intent1); 16 17 break; 18 19 case R.id.dialog: 20 21 Intent intent2 = new Intent(this, DialogActivity.class); 22 23 startActivity(intent2); 24 25 default: 26 27 break; 28 29 } 30 31 } 32 33 @Override 34 35 protected void onCreate(Bundle savedInstanceState) { 36 37 super.onCreate(savedInstanceState); 38 39 setContentView(R.layout.main_layout); 40 41 Log.i("LIFECYCLE","onCreate"); 42 43 Button btnNormal = (Button) findViewById(R.id.normal); 44 45 Button btnDialog = (Button) findViewById(R.id.dialog); 46 47 btnNormal.setOnClickListener(this); 48 49 btnDialog.setOnClickListener(this); 50 51 } 52 53 @Override 54 55 protected void onStop() { 56 57 // TODO Auto-generated method stub 58 59 super.onStop(); 60 61 Log.i("LIFECYCLE","onStop"); 62 63 } 64 65 @Override 66 67 protected void onDestroy() { 68 69 // TODO Auto-generated method stub 70 71 super.onDestroy(); 72 73 Log.i("LIFECYCLE","onDestroy"); 74 75 } 76 77 @Override 78 79 protected void onPause() { 80 81 // TODO Auto-generated method stub 82 83 super.onPause(); 84 85 Log.i("LIFECYCLE","onPause"); 86 87 } 88 89 @Override 90 91 protected void onStart() { 92 93 // TODO Auto-generated method stub 94 95 super.onStart(); 96 97 Log.i("LIFECYCLE","onStart"); 98 99 }100 101 @Override102 103 protected void onRestart() {104 105 // TODO Auto-generated method stub106 107 super.onRestart();108 109 Log.i("LIFECYCLE","onRestart");110 111 }112 113 @Override114 115 protected void onResume() {116 117 // TODO Auto-generated method stub118 119 super.onResume();120 121 Log.i("LIFECYCLE","onResume");122 123 }124 125 }
Running Information:
First, start the application and output it in sequence:
As shown in the lifecycle diagram, onCreate (), onStart (), and onResume () are called in sequence ().
Then, click the first button to start the normal Activity and output it in sequence:
It can be seen that when NormalActivity is started, MainActivity calls onPause () to pause. Because after NormalActivity is started, MainActivity is completely blocked by NormalActivity and onStop () is called to stop.
Then, click the return button of the simulator and output it in sequence:
Visible, because the NormalActivity is destroyed after the return key is pressed, and the MainActivity changes from invisible to visible, onRestart () and onStart () are called in sequence (), because MainActivity is currently at the top of the task stack, onResume () is called to enter the activity status.
Then, click the second button to start the DialogActivity, and output it in sequence:
Because MainActivity is still partially visible, it is no longer at the top of the task stack, so onPause () is called to pause.
Then, press the return button on the simulator and output it in sequence:
DialogActivity is destroyed, MainActivity returns to the top of the stack, and onResume () is called to enter the activity status.
Then, press the return button on the simulator to output the result in sequence:
The MainActivity is to be destroyed. From the activity status to the destruction status, onPause (), onStop (), and onDestroy () are called in sequence ().
The above is a complete Activity lifecycle demonstration.
In addition, because a stopped or paused Activity may be recycled by the system, when an Activity returns to the active state from the paused or stopped status, the Activity may have been recycled in sequence, previous operations, data, and so on, such as filling out a large form, all need to start again, the user experience is very poor. In this case, you need to use the callback function that involves saving the Activity instance status:
OnSaveInstanceState (Bundle bundle): Used to save status information and important data of Activity instances to bundle objects before being recycled by the system. When this Activity instance is created next time and the onCreate (Bundle bundle) method is called, this bundle object will be passed to the onCreate () method. Then, in the onCreate method, obtain the last saved data, initialize the data, and restore the data.