Android: Life cycle case

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.