Android Rookie Learning Note 8----Activity (ii)

Source: Internet
Author: User

About Activity life cycle of :

The following are the methods that are recalled during the activity's entire life cycle, which correspond to the complete life process of the activity.

void OnCreate (Bundle savedinstancestate): Callback when activity is created

void OnStart () : Called after OnCreate () or Onrestart (), which is the first time the activity is created or becomes visible from the invisible state.

void Onresume () : Returns to the active state when the method is bound to be called after OnStart (). The activity is then active, at the top of the stack on the task stack.

void OnPause () : A callback that loses focus but is still partially visible.

void OnStop () : callback when activity becomes completely invisible

void Onrestart () : callback When activity restarts

void OnDestroy () : Callback before activity is destroyed

The above 7 methods, in addition to Onrestart (), are paired in the life cycle diagram. Divided into three pairs, there are three kinds of survival time.

From OnCreate () to OnDestroy (), an activity instance undergoes all the processes created to destroy, called the full lifetime .

From OnStart () to OnStop (), an activity instance becomes invisible from the visible state, which is known as the visible lifetime . Note that the visibility is not necessarily at the top of the stack, and therefore must be able to interact with the user.

From Onresume () to OnPause (), an activity instance undergoes a process between the active state and the paused state, both of which are active, called foreground lifetimes , or active status lifetimes .

Full Life Cycle program demo, refer to the first line of code

The program has three activity:mainactivity is the entrance, placed two buttons, respectively, to start another two activity, to achieve 7 life cycle callback method, output a running message; Normalactivity is a normal activity. ; dialogactivity configured the theme property in Manifest.xml to make it a dialog-style activity,<activity android:name= ". Dialogactivity "Android:theme=" @android: Style/theme.dialog "></activity>.

Specific code:

Main_layout.xml:

1 <?XML version= "1.0" encoding= "Utf-8"?>2 3 <LinearLayoutxmlns: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" >Ten  One     <Button A  -         Android:id= "@+id/normal" -  the Android:layout_width= "Wrap_content" -  - Android:layout_height= "Wrap_content" -  + Android:text= "@string/start_normal_activity"/> -  +     <Button A  at         Android:id= "@+id/dialog" -  - Android:layout_width= "Wrap_content" -  - Android:layout_height= "Wrap_content" -  in Android:text= "@string/start_dialog_activity"/> -  to </LinearLayout>

Mainactivity.java:

1  Public classMainactivityextendsActionbaractivityImplementsOnclicklistener {2 3 @Override4 5        Public voidOnClick (view view) {6 7            //TODO auto-generated Method Stub8 9            Switch(View.getid ()) {Ten  One             CaseR.id.normal: A  -Intent intent1 =NewIntent ( This, Normalactivity.class); -  the startactivity (intent1); -  -                   Break; -  +             CaseR.id.dialog: -  +Intent Intent2 =NewIntent ( This, Dialogactivity.class); A  at startactivity (intent2); -  -            default: -  -                   Break; -  in            } -  to       } +  - @Override the  *     protected voidonCreate (Bundle savedinstancestate) { $ Panax Notoginseng         Super. OnCreate (savedinstancestate); -  the Setcontentview (r.layout.main_layout); +  ALOG.I ("LIFECYCLE", "OnCreate"); the  +Button Btnnormal =(Button) Findviewbyid (r.id.normal); -  $Button Btndialog =(Button) Findviewbyid (r.id.dialog); $  -Btnnormal.setonclicklistener ( This); -  theBtndialog.setonclicklistener ( This); - Wuyi     } the  - @Override Wu  -       protected voidOnStop () { About  $            //TODO auto-generated Method Stub -  -            Super. OnStop (); -  ALOG.I ("LIFECYCLE", "OnStop"); +  the       } -  $ @Override the  the       protected voidOnDestroy () { the  the            //TODO auto-generated Method Stub -  in            Super. OnDestroy (); the  theLOG.I ("LIFECYCLE", "OnDestroy"); About  the       } the  the @Override +  -       protected voidOnPause () { the Bayi            //TODO auto-generated Method Stub the  the            Super. OnPause (); -  -LOG.I ("LIFECYCLE", "OnPause"); the  the       } the  the @Override -  the       protected voidOnStart () { the  the            //TODO auto-generated Method Stub94  the            Super. OnStart (); the  theLOG.I ("LIFECYCLE", "OnStart");98  About       } - 101 @Override102 103       protected voidOnrestart () {104  the            //TODO auto-generated Method Stub106 107            Super. Onrestart ();108 109LOG.I ("LIFECYCLE", "Onrestart"); the 111       } the 113 @Override the  the       protected voidOnresume () { the 117            //TODO auto-generated Method Stub118 119            Super. Onresume (); - 121LOG.I ("LIFECYCLE", "Onresume");122 123       }124  the}

Operational information:

Start the application first, and then click Output:

Visible, as shown in the life cycle Diagram, OnCreate (), OnStart (), Onresume () are called in turn.

Then click on the first button to start the normal activity, then output:

It is visible that when normalactivity is started, mainactivity calls OnPause () into a paused state, and because mainactivity is completely obscured by normalactivity after Normalactivity is started, Also call OnStop () to enter the stop state.

Then, click the emulator's Back button and output:

It can be seen that the normalactivity is destroyed when the return key is pressed, the mainactivity becomes visible by the invisible State, then Onrestart (), OnStart () are called, and because Mainactivity is currently at the top of the task stack, So call Onresume () to enter the active state.

Then, click the Second button, start dialogactivity, and then output:

Since mainactivity is still partially visible, it is no longer at the top of the task stack, so OnPause () is called into the paused state.

Then, press the Back button on the emulator to output:

Dialogactivity is destroyed, mainactivity back to the top of the stack, calling Onresume () to enter the active state.

Then, press the Back button on the emulator, then output:

Mainactivity to be destroyed, from active state to destroyed state, called OnPause (), OnStop (), and OnDestroy () in turn.

The above is a complete activity life cycle demo.

In addition, because the activity of the stop state and the suspended state is likely to be reclaimed by the system, when an activity from the suspended or stopped state back to the active state, because it may have been recycled, the previous operation, data, etc., such as fill in a good big form, all to start again, The user experience is extremely poor. At this point, you need to use a callback function that involves the state of the activity instance:

Onsaveinstancestate (Bundle bundle): Used to save some activity instance state information, important data, etc. to the bundle object before being reclaimed by the system. When the activity instance is next created, when the OnCreate (bundle bundle) method is called, the bundle object is passed to the OnCreate () method, and in the OnCreate method, the last saved data can be obtained and initialized accordingly. Resume work.

Android Rookie Learning Note 8----Activity (ii)

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.