Several questions about the Android activity life cycle

Source: Internet
Author: User

Every Android developer should know that the Android system has four important basic components: activity (Active), service (services), broadcast receive (broadcast receiver), and content Provider (contents provider , where activity is the most important component, open the app on your phone, all you can see is activity, and here are a few questions about the activity life cycle.

1 8 callback functions for the activity life cycle

Is the activity of life cycle diagram, I believe many people have seen more than once, but some people see this picture when it is clear that after a period of time can not remember or forget, how to fully understand and master it?

    1. 650) this.width=650; "Src=" http://img.blog.csdn.net/20170309234828576?watermark/2/text/ ahr0cdovl2jsb2cuy3nkbi5uzxqvdghld2fsa2vymzawma==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/ Gravity/center "style=" border:0px; "/>

Actually very simple, just remember the following two points on the line:

(1) Activity of 6 callback functions OnCreate () –onstart () –onresume () –onpause () –onstop () –ondestroy () can be divided into three groups, inside the Onresume () and Onpaus E () Indicates whether the activity receives focus at the foreground, OnStart () and onstop () indicate whether activity is visible, and the outermost oncreate () and OnDestroy () indicate whether the activity exists. The Onrestart () function is not called when the activity is first started, and when the activity returns to the visible state from the OnStop () invisible state, it calls Onrestart () and then OnStart (), in addition to the 7 callback functions, One more important function is onsaveinstancestate (), which is often used to save actitity state variables.

(2) After the activity normal start, will stop after the Onresume () function, imagine that you open the Android phone app, is looking at an activity above the content shown, if the activity print out debugging information and you can see, Then the approximate information will be as shown

    • 650) this.width=650; "Src=" http://img.blog.csdn.net/20170309235044062?watermark/2/text/ ahr0cdovl2jsb2cuy3nkbi5uzxqvdghld2fsa2vymzawma==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/ Gravity/center "width=" 498 "height=" "style=" border:0px; "/>

When you look at the contents of the activity, the activity stops after the Onresume function.

2 description of several important callback functions for activity

What is the most important of the 8 callback functions of the activity mentioned above?

OnCreate () is definitely the most important, the IDE will generate the OnCreate () function By default, it will be called when the activity is first created, the layout will load in the OnCreate () function, initialize the view, bind events and other initialization operations, A few other callback functions of the activity may not be called, but OnCreate () must be called, because OnCreate () is often a lot of content, so do not write all the code together, so it is not easy to maintain, but should be divided into various sub-functions, similar to the following way:

Protected voidoncreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);                  Setcontentview (r.layout.activity);                  F1 ();                  F2 ();                  F3 (); ...}

OnPause () is the first method that is called when you leave the activity, you should save some important data in onPause (), freeing up the system resources, because only OnPause () _ executes, the next activity will run properly, so OnPause () Time-consuming operations cannot be performed.

Onsaveinstancestate () is often used to hold temporary state information for activity, which is useful when the system configuration information changes or the memory is low causing the activity to be rebuilt. For example, the information we enter in the EditText, when the screen rotates, the information is lost, in this case, we can save the user input data in onsaveinstancestate (), and then in the OnCreate () or onrestoreinstancestate () to recover, the latter is more convenient because there is no need to make a null value judgement.

3 Other minor problems

(1) About Super.oncreate (savedinstancestate)

You will find that each callback function first calls the parent class's callback function, Super.onx (), which is required, and if the parent class's callback function is not called, the runtime application crashes.

(2) Why don't you see the activity's constructor

In fact, you can write the activity's constructor, the parameterless constructor does execute before OnCreate (), but the constructor does not really work, the system creates the context after the activity constructor executes, and then calls the OnCreate () function. All initialization operations should be placed in OnCreate ().

(3) When will only call OnPause () and will not call OnStop ()

OnPause () indicates that activity loses focus, onStop () means invisible, so it must be the activity loses focus but partially visible to call OnPause without calling OnStop (). But not all lose focus and partially visible will call OnPause (), such as the current Activity Generated dialog box, the System's Shutdown dialog box, the Volume dialog box, all the current activity loses focus, but will not call the OnPause () function.

How can I call OnPause () without calling OnStop? You can create a new two activity, start the Activtiy2 with Activity1, and set the Activity2 theme to

Android:theme= "@style/theme.appcompat.dialog"

You will find that Activity1 only calls OnPause () and does not call OnStop.

When you open a URL in the activity is, the system will pop up a dialog box, there will be a few apps let you choose Ze, such as UC Browser or QQ browser, and so on, it will only call OnPause (), not yet called OnStop (). Concrete method Everyone can test their own experiments.

These are a few questions about the activity life cycle, and other topics about activity can be discussed later.

Reprint please indicate the source

You are welcome to comment


Several questions about the Android activity life cycle

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.