The activity life cycle of Android four components

Source: Internet
Author: User

Before writing this blog post, have a certain understanding and understanding of Android. This blog post focuses on the life cycle of Android activity, which is what Android developers need to know. Android's activity component has 7 lifecycles: OnCreate (), OnStart (), Onresume (), OnPause (), OnStop (), Ondestory (), Onrestart ().

These 7 lifecycles are called at 4 specific stages, respectively.

Start activity: Perform 3 lifecycles in turn when activity starts: OnCreate, OnStart, Onresume.

Activity loses focus: If you enter another focus from one activity, the current activity executes: OnPause, OnStop.

Activity regain focus: When returning this activity: Onrestart, OnStart, Onresume.

Closed Activity:activity is closed: OnPause, OnStop, Ondestory.

Activity life cycle Flowchart:

See two loops of activity life cycle: 1.onResume, OnPause, Onresume;2.onstop, Onrestart, OnStart, Onresume, OnPause, OnStop

There are three life cycles of activity:

Overall life cycle: OnCreate, OnStart, Onresume, OnPause, OnStop, ondestory

Visual life cycle: OnStart, Onresume, OnPause, OnStop

Focus life cycle: Onresume, OnPause

Code to verify Android life cycle

    protected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); LOG.D ("Test", "OnCreate"); } @Overrideprotected voidOnStart () {//TODO auto-generated Method Stub        Super. OnStart (); LOG.D ("Test", "OnStart"); } @Overrideprotected voidOnresume () {//TODO auto-generated Method Stub        Super. Onresume (); LOG.D ("Test", "Onresume"); } @Overrideprotected voidOnPause () {//TODO auto-generated Method Stub        Super. OnPause (); LOG.D ("Test", "OnPause"); } @Overrideprotected voidOnStop () {//TODO auto-generated Method Stub        Super. OnStop (); LOG.D ("Test", "OnStop"); } @Overrideprotected voidOnDestroy () {//TODO auto-generated Method Stub        Super. OnDestroy (); LOG.D ("Test", "Ondestory"); } @Overrideprotected voidOnrestart () {//TODO auto-generated Method Stub        Super. Onrestart (); LOG.D ("Test", "Onrestart"); } @Override Public BooleanOncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (R.menu.main, menu); return true; } @Override Public Booleanonoptionsitemselected (MenuItem item) {//Handle Action Bar item clicks here. The Action Bar would//automatically handle clicks on the Home/up button, so long//As you specify a the parent activity in Androidmanifest.xml.        intID =Item.getitemid (); if(id = =r.id.action_settings) {            return true; }        return Super. onoptionsitemselected (item); }
Life cycle Testing

The program just started running

Program loses focus

The activity life cycle of Android four components

Related Article

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.