Activity life cycle and startup mode

Source: Internet
Author: User

One of the most fascinating aspects of Android development is the ability to drag a few controls and run the code to see what's on the page. The power of activity is reflected in it. Landlord used to do web development, was more than 2000 lines of JS code to get dizzy. Then occasionally once a chance, see Android Drag control to show the video, was attracted into the door, embarked on the android of no return. Android development in the past few years, the spirit of the code can be copied not to do their own spirit, walked a lot of detours. Now with a number of learning books, experience, understand the original rational things, convenient next copy (^_^).

Needless to say, the activity of the life cycle diagram ( This is often asked in the interview, Ah, I do not know what to ask )

Life cycle of activity

Image original Address (http://cs.szpt.edu.cn/android/guide/components/activities.html)

Activity from creation to destruction, the normal flow of life is Oncreate->onstart->onresume->onpause->onstop->onstop->ondestroy.

But cheese in the development, I will meet to not take the usual road.

Life cycle Analysis

Oncreate:activity is being created, is the first method of life cycle, the general initialization of the work is done here, such as Setcontentview load layout, getintent get the last page passed the data, Findviewbyid, Setlistener, etc.

Onrestart:activity is restarting. In general, the Onrestart method is executed when the application changes from a daemon to a foreground program.

Onstart:activity is starting, the activity is visible in the background of the system and the user cannot manipulate the elements on the interface.

The onresume:activity is visible and appears in the foreground. And onstart The difference is: OnStart, the activity in the background, onresume when the activity is displayed to the foreground.

The onpause:activity is in a paused state and the user is unable to operate.

Onstop:activity is about to stop and can do a small amount of recycling, but it can't be too time consuming.

Ondestroy:activity is about to be destroyed, and can be used for recovery work and release of resources. The registered broadcast recipient is generally cancelled by unregisterreceiver, Handler.removemessages, timer cancel, etc.

Test code
 PackageCom.chunjiangchao.demo;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.util.Log; Public classMainactivityextendsActivity {Private Static FinalString tag = "Mainactivity"; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); LOG.I (Tag,"OnCreate"); } @Overrideprotected voidOnStart () {Super. OnStart (); LOG.I (Tag,"OnStart"); } @Overrideprotected voidOnrestart () {Super. Onrestart (); LOG.I (Tag,"Onrestart"); } @Overrideprotected voidOnresume () {Super. Onresume (); LOG.I (Tag,"Onresume"); } @Overrideprotected voidOnPause () {Super. OnPause (); LOG.I (Tag,"OnPause"); } @Overrideprotected voidOnStop () {Super. OnStop (); LOG.I (Tag,"OnStop"); } @Overrideprotected voidOnDestroy () {Super. OnDestroy (); LOG.I (Tag,"OnDestroy"); }    }

Configuration in the Androidmanifest.xml

        <activity            android:name= "com.chunjiangchao.demo.MainActivity"            android:label= "@ String/app_name ">            <intent-filter>                <action android:name=" Android.intent.action.MAIN "/>                <category android:name= "Android.intent.category.LAUNCHER"/>            </intent-filter>        </ Activity>
View Code

Normal process testing:

Start the activity and click the Back button to destroy the following life cycle:

After activating the activity, click the Home button

Rotating the life cycle of a screen

Not to be continued (or the night to write it, the morning really do not come out AH)

Activity life cycle and startup mode

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.