In the chapter on learning the Android app life cycle, the book is a little general and difficult to understand. In order to understand the deeper, wrote a program to test a bit.
1. Build a dialog_layout.xml in the layout folder
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/ Res/android " android:layout_width=" match_parent " android:layout_height=" match_parent " android:o rientation= "vertical" > <textview android:layout_width= "match_parent" android:layout_ height= "Wrap_content" android:text= "This is a dialog Activity." /></linearlayout>
2. Build another normal_layout.xml in the layout folder
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/ Res/android " android:layout_width=" match_parent " android:layout_height=" match_parent " android:o rientation= "vertical" > <textview android:layout_width= "match_parent" android:layout_ height= "Wrap_content" android:text= "This is a normal Activity." /></linearlayout>
3. Modify the Activity_main.xml in the layout folder
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayoutxmlns:android= "http://schemas.android.com/apk/res/ Android "android:orientation=" vertical "android:layout_width=" match_parent "android:layout_height=" Match_parent " > <button android:id= "@+id/start_normal_activity" android:layout_width= "Match_parent " android:layout_height= "Wrap_content" android:text= "Start normalactivity" /> <button Android:id= "@+id/start_dialog_activity" android:layout_width= "match_parent" android:layout_height= " Wrap_content " android:text=" Start dialogactivity " /> </LinearLayout>
4, write Java files, establish a Dialogactivity.java
Package Com.example.acitivitylife;import Android.app.activity;import Android.os.bundle;public class DialogActivity Extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.dialog_layout);}}
5, write Java files, establish a Normalactivity.java
Package Com.example.acitivitylife;import Android.app.activity;import Android.os.bundle;public class NormalActivity Extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.normal_layout);}}
6, modify the Mainactivity.java
Package Com.example.acitivitylife;import Android.app.activity;import Android.content.intent;import Android.os.bundle;import Android.util.log;import Android.view.view;import Android.view.View.OnClickListener; Import Android.widget.button;public class Mainactivity extends Activity {public static final String TAG = "mainactivity"; @ overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); LOG.D (TAG, "onCreate"); Setcontentview (R.layout.activity_main); Button startnormalactivity = (button) Findviewbyid (r.id.start_normal_activity); Button startdialogactivity = (button) Findviewbyid (r.id.start_dialog_activity); Startnormalactivity.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {//TODO Auto-generated method Stubintent Intent = new Intent (mainactivity.this, Normalactivity.class); startactivity (intent);}}); Startdialogactivity.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {//TODO Auto-generated methodStubintent Intent = new Intent (mainactivity.this, Dialogactivity.class); startactivity (intent);}});} @Overrideprotected void OnStart () {Super.onstart (); LOG.D (TAG, "OnStart");} @Overrideprotected void Onresume () {super.onresume (); LOG.D (TAG, "Onresume");} @Overrideprotected void OnPause () {super.onpause (); LOG.D (TAG, "OnPause");} @Overrideprotected void OnStop () {super.onstop (); LOG.D (TAG, "OnStop");} @Overrideprotected void OnDestroy () {Super.ondestroy (); LOG.D (TAG, "OnDestroy");} @Overrideprotected void Onrestart () {Super.onrestart (); LOG.D (TAG, "Onrestart");}}
After completing the above steps, install to the phone and watch the Logcat print out the message, the life cycle will be at a glance.
After automatically installing to the phone, logcat prints out:
When you click the Start Normalgactivitya button, Logcat prints out:
After pressing the back key, Logcat prints out:
When you click the Start Dialogactivitya button, Logcat prints out:
After pressing the back key, Logcat prints out:
After exiting the program, Logcat prints out:
Android: Life cycle case