Android Learning notes (vi)

Source: Internet
Author: User

Life cycle of activities

Activities in Android can overlap, and each launch of a new activity will overwrite the original activity and then click the back key to destroy the top activity.

Android uses tasks to manage activities, and a task is a set of activities that are stored in the stack, also known as the back stack.

Each activity may have a maximum of four states in its life cycle:

1. Operational status

When an activity is at the top of the stack at the back of the stack, the activity is in the running state.

2. Pause Status

An activity is no longer at the top of the stack, but is still visible, when the activity enters a paused state.

3. Stop state

An activity is no longer at the top of the stack, and it is completely invisible when it enters the stop state.

4. Destruction Status

When an activity is removed from the return stack, it becomes the destroy state.

The activity class defines seven callback methods that cover every aspect of the lifecycle:

1.onCreate () method

It is called when the activity is first created, in which the initialization of the activity is completed, such as loading the layout, binding events, and so on.

2.onStart () method

This method is called when the activity becomes visible from invisible.

3.onResume () method

This method is called when it is ready to interact with the user.

4.onPause () method

This method is called when the system is ready to start or resume another activity.

5.onStop () method

This method is called when the activity is completely invisible. The main difference between it and the OnPause () method is that if a new activity is started that is a dialog box activity, the OnPause () method is called, and the OnStop () method does not execute.

6.onDestroy () method

This method is called before the activity is destroyed.

7.onRestart () method

The method is called before the activity is changed from the stopped state to the running state, and the activity is restarted.

Attach the life cycle of an activity that is officially provided by Android:

Create a new Android project that automatically creates activities and layouts when you create a new one.

Create a new Normal_layout.xml file with the following code:

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical" >        <TextViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "@string/normal_activity"         /></LinearLayout>

In this layout, a single TextView is used to display a single line of text.

In creating a new Dialog_layout.xml file, the code is as follows:

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical" >        <TextViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "@string/dialog_activity"         /></LinearLayout>

Then the new normalactivity inherits from the activity with the following code:

 Packagecom.gfeng.activitylifecycletest;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.view.Window; Public classNormalactivityextendsActivity {protected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Requestwindowfeature (Window.feature_no_title);    Setcontentview (r.layout.normal_layout); }}

Loads the Normal_layout layout in the Normalactivity class.

In the new dialogactivity inherit from activity, the code is as follows:

 Packagecom.gfeng.activitylifecycletest;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.view.Window; Public classDialogactivityextendsActivity {protected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Requestwindowfeature (Window.feature_no_title);    Setcontentview (r.layout.dialog_layout); }}

Loads the Dialog_layout layout in the Dialogactivity class.

The

Finally registers the normalactivity and dialogactivity activities separately in the Androidmanifest.xml file. Add the following code:

 <  activity  android:name  = ". Normalactivity " >  </ activity  >  <  activity  =". Dialogactivity "  Android:theme  =" @andro Id:style/theme.dialog " >  </
    activity  >  

Here, unlike the registration code for two activities, Dialogactivity uses the Android:theme property, which specifies the subject for the current activity.

Modify the activity_main.xml below, re-customize the layout of the main activity, modify the code as follows:

<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical" >    <ButtonAndroid:id= "@+id/start_normal_activity"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "@string/start_normal_activity" />    <ButtonAndroid:id= "@+id/start_dialog_activity"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "@string/start_dialog_activity" />    </LinearLayout>

Use LinearLayout here, add two buttons, one to start normalactivity, and another to start dialogactivity.

Then modify the code in the mainactivity:

1  Packagecom.gfeng.activitylifecycletest;2 3 Importandroid.app.Activity;4 Importandroid.content.Intent;5 ImportAndroid.os.Bundle;6 ImportAndroid.util.Log;7 ImportAndroid.view.View;8 ImportAndroid.view.View.OnClickListener;9 ImportAndroid.view.Window;Ten ImportAndroid.widget.Button; One  A  -  Public classMainactivityextendsActivity { -  the      Public Static FinalString TGA = "Mainactivity"; -      - @Override -     protected voidonCreate (Bundle savedinstancestate) { +         Super. OnCreate (savedinstancestate); -LOG.D (TGA, "OnCreate"); + requestwindowfeature (window.feature_no_title); A Setcontentview (r.layout.activity_main); atButton startnormalactivity =(Button) Findviewbyid (r.id.start_normal_activity); -Startnormalactivity.setonclicklistener (NewOnclicklistener () { -              - @Override -              Public voidOnClick (View v) { -                 //display the use of the intent method inIntent Intent =NewIntent (mainactivity. This, Normalactivity.class); - startactivity (intent); to                  +             } -         }); the          *Button startdialogactivity =(Button) Findviewbyid (r.id.start_dialog_activity); $Startdialogactivity.setonclicklistener (NewOnclicklistener () {Panax Notoginseng              - @Override the              Public voidOnClick (View v) { +                 //display the use of the intent method AIntent Intent =NewIntent (mainactivity. This, Dialogactivity.class); the startactivity (intent); +                  -             } $         }); $          -     } -      the     protected voidOnStart () { -         Super. OnStart ();WuyiLOG.D (TGA, "OnStart"); the     } -      Wu     protected voidOnresume () { -         Super. Onresume (); AboutLOG.D (TGA, "Onresume"); $     } -      -     protected voidOnPause () { -         Super. OnPause (); ALOG.D (TGA, "OnPause"); +     } the      -     protected voidOnStop () { $         Super. OnStop (); theLOG.D (TGA, "OnStop"); the     } the      the     protected voidOnDestroy () { -         Super. OnDestroy (); inLOG.D (TGA, "OnDestroy"); the     } the      About     protected voidOnrestart () { the         Super. Onrestart (); theLOG.D (TGA, "Onrestart"); the     } +}

In the OnCreate () method, a click event is registered for each of the two buttons, and clicking the first button launches the Normalactivity, and clicking the second button launches the dialogactivity. Then print a single word in the activity's seven callback methods, in the log

To understand the life cycle of an activity.

Run the program, Effect:

Observing the print log in Logcat, you can get the following:

From the log you can see that the OnCreate (), OnStart (), Onresume () methods are executed sequentially when Mainactivity is created for the first time.

Click on a button to start normalactivity and get the following:

At this point, view the Logcat print log to get:

The OnPause () and OnStop () methods will be executed because the normalactivity completely blocks the mainactivity. Then press the back key to return to Mainactivity,logcat to print the log information as shown in:

Since Mianactivity has entered a stop state before, the Onrestart () method is executed, followed by the OnStart () and the Onresume () method.

Then click on the second button to start Dialogactivity, as shown in:

When you look at the printed information for Logcat, you get the information as shown:

Since Dialogactivity does not completely block mainactivity, Mainactivity only enters the paused state, only the OnPause () method will be executed.

Correspondingly, pressing the back key returns mainactivity and only the Onresume () method will be executed. As shown in the following:

Finally, under the Mainactivity interface, press the back button to exit the program, to view the Logcat printing information, you will get the following:

The OnPause () and OnStop () methods are executed at once. The program does not implement the OnDestroy () method, and according to the life cycle diagram of the activity given by the official, you can see that the app process killed processes that can destroy the program.

Android Learning notes (vi)

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.