Android Rookie note 2-activity life cycle and log

Source: Internet
Author: User

One:acticity

Activity is one of the 4 major components of Android programs.

Activity is the presentation layer of the Android program. Each display screen of a program is an Activity.

Students who have studied Web development can interpret Activity as a JSP file in a Web page, or you can interpret it as a Windows window.

ActivityIsAndroidThe display layer of the program, each display window is aActivity; butActivityitself cannot be displayed on the screen, we can interpret it as an abstraction layer, a shell; for example, aJSPThe page, which itself does not show anything, is responsible for showing that he generated theHTMLLabel. SoAndroidWho is the real part of the show? --IsViewAndViewGroup, whileViewGroupActually, too.ViewSub-class.

With the above concepts, we can now speak clearly of aActivityHow the display elements in the display are displayed. First of allUIComponents are progressively presented in a way that is out-of-order from the hierarchy. To bind a screen element hierarchy tree on one screen,ActivityWill call it'sSetcontentview ()Method and pass in the root node reference of the hierarchy tree. WhenActivityis activated and the focus is received, the system notifiesActivityand the root node is asked to compute and draw the tree, and the root node requests its child nodes to draw themselves. On each tree.ViewGroupThe node is responsible for drawing its child nodes.ViewGroupIt calculates its effective space, layouts all the child display objects, and eventually calls all of the child display objectsDraw ()method to draw the display object. Each child display object can request their size and position in the layout to the parent object, but ultimately the size and position of each child display object is the parent object.

The Android program uses the View and viewgroup objects to build the user interface. Android offers much more than HTML , out-of-the-box user interface components, such as the most popular pentagram scoring effect component on the site now ratingbar.


II : Activity life cycle

The most important and basic function of the phone is to call, it means that the phone may be at any time to suspend the current program, if the battery is not enough time may be at any time to close the existing program, so the Android program and the computer program is different, specific to Activity, theactivity life cycle is not self-control, but is controlled by the Android system.

There are four basic statuses of Activity in android :


1,Running

At the front of the screen, the state is visible, and the user can interact.

2,Paused

When Acitivy is overwritten by another transparent or non-full-screen Activity , the state is called the Paused state, although visible but not interactive.

3.Stop

When activity is overwritten by another activity and the interface is not visible, it is in the Stop state.

4,killed

The Activity was killed by the system or was killed when it was not started.


Three: Log

Android provides its own log output api--> located in the Android.util.Log class .

This class compares the common print log method to have 5 , these 5 methods will print the log to the LogCat :

log.v (tag,message);//verbose mode , print the most detailed log

log.d (tag,message);//debug -level logs

log.i (tag,message);//info -level logs

LOG.W (tag,message);//warn -level logs

LOG.E (tag,message);//error -level logs

where tag and message are two String values, respectively .

ag is used to mark the source of log messages Span style= "Font-family:times New roman,serif" >. and message is this log content

I changed the various methods of Activity , adding log.d to each method to record the output of the log , as a result .



Run Project, view LogCat output


Rotate the app 's screen to view the log


We can see that when the screen flips, theAndroid system kills the activitylife Activity First (in the order that it is paused, then closed and then destroyed), and then started (the exact order is created first, After starting and resuming). By this example, we also see clearly that Android is the system, not the programmer, that controls the life cycle of the Activity .



Android Rookie note 2-activity life cycle and log

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.