Android Activity Learning Notes (i)

Source: Internet
Author: User

Activity is one of the four components of Android apps, providing an interface to interact with users. The knowledge of the life cycle of the activity is here to make a note.

The life cycle of an activity is made up of the following parts:

1.oncreate:activity launch is called when the layout and resource settings are completed.

2.onstart:activity starts to be visible, but cannot interact with the user (example below).

The 3.onresume:activity is visible, and the interaction can be done with the user at this time.

4.onpause:activity will not be visible or otherwise called when dialog activity is foreground.

Called when the 5.onstop:activity is completely invisible, the activity enters the background.

6.ondestroy:activity in the background, called by the finish or kill.

7.onRestart: Called when the OnStop state becomes visible again.

The activity's life cycle diagram looks like this:

For ease of understanding, write a simple code to understand the activity's life cycle:

 PackageCom.example.steve.myfirstapp;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.os.Bundle;ImportAndroid.util.Log; Public classMainactivityextendsappcompatactivity {Private Static FinalString TAG = "Myfirstapp"; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); //The layout file has only one TextView, the default value is "Hello world! " log.d (TAG,"OnCreate"); } @Overrideprotected voidOnrestart () {log.d (TAG,"Onrestart"); Super. Onrestart (); } @Overrideprotected voidOnStart () {log.d (TAG,"OnStart"); / * Let process sleep 5stry {thread.currentthread (). Sleep (5000);        } catch (Interruptedexception e) {e.printstacktrace ();        } log.d (TAG, "OnStart Sleep 5s"); */        Super. OnStart (); } @Overrideprotected voidOnresume () {log.d (TAG,"Onresume"); Super. Onresume (); } @Overrideprotected voidOnPause () {log.d (TAG,"OnPause"); Super. OnPause (); } @Overrideprotected voidOnStop () {log.d (TAG,"OnStop"); Super. OnStop (); } @Overrideprotected voidOnDestroy () {log.d (TAG,"OnDestroy"); Super. OnDestroy (); }}

During the actual operation, when the Android app is opened, the mainactivity will pass through Onresume three stages, OnCreate, OnStart, if you press the home key or the back button, Mainactivity will go through the two stages of onpause---onstop, and then into this mainactivity, will call Onrestart, OnStart, onresume three stages.

After the Android app is paged out via the recent button, Mainactivity will eventually be called to the ondestory stage after attempting to clear the Android app.

There is a comment in the OnStart code, this code is mainly used to let the main thread sleep 5s, this operation is used to understand that "activity is visible, but not to interact with the user."

When you open the Android app, Mainactivity will enter the Onresume phase, OnCreate-I, OnStart. When attempting to do a delay operation on the main thread during the OnStart phase, the activity renders like this:

Activity is visible at this point, but it cannot interact with the user. When entering the onresume phase, the activity appears as shown in the following:

At this point, the activity is not only visible, but it can also be done interactively with the user (it is easier to understand if you add a button to the Layout.xml file).

Reference: Https://github.com/codepath/android_guides/wiki/Activity-Lifecycle

Android Activity Learning Notes (i)

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.