The life cycle of Android activity

Source: Internet
Author: User

Learning Android does not learn life cycle estimates are also not mixed, the original learning asp.,net when the life cycle is very painful, but fortunately Android activity life cycle than the ASP. NET application life cycle is relatively good understanding, Reference to Google's API, as well as the Chinese blog, plus their own point of view, beginner Android, Master can ignore me ~

First look at a very classic picture:

The activity inherits the ApplicationContext and goes to the definition and can rewrite the method:

* Public class Activity extends ApplicationContext {*     protected void onCreate (Bundle savedinstancestate); * *     Pro tected void OnStart (); *      *     protected void Onrestart (); * *     protected void Onresume (); * *     protected void onPause (); * *     Prote CTED void OnStop (); * *     protected void OnDestroy (); *}

A new lifecycle project has been created, overriding the above methods in Mainactivity:

public class Mainactivity extends Activity {private static final String TAG = "com.example.lifecycle"; @Override Pro        tected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main);    LOG.I (TAG, "life cycle create");    } @Override protected void OnStart () {//TODO auto-generated Method Stub Super.onstart ();    LOG.I (TAG, "life cycle start");    } @Override protected void Onresume () {//TODO auto-generated Method Stub super.onresume ();    LOG.I (TAG, "life cycle Resume");    } @Override protected void OnPause () {//TODO auto-generated Method Stub super.onpause ();    LOG.I (TAG, "life cycle pause");    } @Override protected void OnStop () {//TODO auto-generated Method Stub super.onstop ();    LOG.I (TAG, "Life cycle Stop");    } @Override protected void OnDestroy () {//TODO auto-generated Method Stub Super.ondestroy ();    LOG.I (TAG, "Life cycle Destroy"); } @Override ProtectEd void Onrestart () {//TODO auto-generated Method Stub Super.onrestart ();    LOG.I (TAG, "Life cycle restart");    } public void Test (view view) {Intent intent=new Intent (this,useractivity.class);    StartActivity (Intent); }}

Run as shown:

Open executed OnCreate ()->onstart ()->onresume three methods:

If this time use the back key, call Order OnPause ()->onstop ()->ondestory ():

Home key:

If a call is made at this time when using an activity, the activity is not destroyed, just paused:

If you press the home button again, you'll see it. executed Onrestart ()->onstart ()->onresume ():

A Click event has been added to the activity to set the useracitivity first: 

      Android:theme= "@android: Style/theme.dialog"

Post-click Effects:

This time to see log,useractivity appear in front of main, but main is still visible, so this time just executed the OnPause method, and did not execute OnStop method, Onresume is a recovery start action, when the user interface can be clicked, Can be entered, to operate, with the OnPause is the corresponding ~

Beginner Android, if inappropriate, please correct me ~

The life cycle of Android activity

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.