Activity life cycle-the latest experiment-based summary

Source: Internet
Author: User

do not say too much nonsense, first on the two most basic picture



Simply explain:1. OnCreate ()
It is called when the activity is created for the first time. You should complete the initialization of the activity in this method, such as loading the cloth
Office, binding events, and so on.
2. OnStart ()
This method is called when the activity becomes visible from invisible.
3. Onresume ()
This method is called when the activity is ready to interact with the user. The activity at this time must be on the return stack
Top of the stack, and is in a running state.
4. OnPause ()< Span style= "Background-color:inherit" > < Span style= "Background-color:inherit" >
cpu  resources are freed, and some key data is saved, but the execution speed of this method
must be fast, otherwise it will affect the use of new stack top activities.
/span> 5. OnStop () < Span style= "Background-color:inherit" >
< Span style= "Background-color:inherit" This method is called when the activity is completely invisible. The main difference between it and  onpause () onpause () onstop ()
method does not execute.
/span> 6. OnDestroy () < Span style= "Background-color:inherit" > < Span style= "Background-color:inherit" > < Span style= "Background-color:inherit" >
< Span style= "Background-color:inherit" This method is called before the activity is destroyed, and then the state of the activity becomes the destroy state.
/span> 7. Onrestart () < Span style= "Background-color:inherit" > < Span style= "Background-color:inherit" > < Span style= "Background-color:inherit" > < Span style= "Background-color:inherit" >

8. Onsaveinstancestate:When an activity becomes "easy" to be destroyed by the system, the activity's onsaveinstancestate is executed unless the activity is actively destroyed by the user, such as when the user presses the back key.  
Notice the double quotes above, what is "easy"? The implication is that the activity has not been destroyed, but merely a possibility. What are the possibilities?1, the following scenario 2: A normal activity start, and then press the home button (or long press the home button to start other programs) 2, the following scene 3: A normal activity starts, and then press the Power key to turn off the screen display 3, the following scene 5: Start a new Activity4, The following scenario 6: Toggle Screen
In summary, Onsaveinstancestate's invocation follows an important principle that when the system destroys your activity "without your permission", then Onsaveinstancestate is called by the system, which is the responsibility of the system, Because it has to provide an opportunity for you to save your data (of course you don't save it then just do it yourself).  9. Onrestoreinstancestate:It should be noted that the Onsaveinstancestate method and the Onrestoreinstancestate method "not necessarily" are called in pairs, and onrestoreinstancestate is invoked on the premise that activity A "does" Was destroyed by the system (seeScenario 6), and if it is only possible to stay in the case, then the method will not be called, for example, when activity A is being displayed, the user presses the home key back to the main interface, and the user then returns to activity a, in which case the activity A will not be destroyed by the system for memory reasons, so activity A's Onrestoreinstancestate method will not be executed. In addition, the bundle parameters of the Onrestoreinstancestate are passed to the OnCreate method, and you can also choose to do a data restore in the OnCreate method.

Scenario Validation:Scenario 1, a normal activity from boot to destruction (back), the ellipse is started, the block is destroyed
Scenario 2 , a normal activity starts, and then press the Home key (or press and hold the home key to start another program)

Reboot again

Scenario 3 , a normal activity is started, then press the Power key to lock the screen, and then open the screen Jianping (found in fact and press the home button)

Scenario 4 , a normal activity starts, and then the drop-down status bar, the life cycle is unchanged

At this point, click the notification in the notification bar to go to another app, and then go back

Scenario 5, click the button in Mainactivity to enter the next activity (named Activityone), and then return in two cases: the first:

The second type:



Scenario 6,when you do not set the activity's android:configchanges, the screen will recall the life cycle, cut the screen will be executed once, cut the vertical screen is only performed once


Scenario 7, set the activity's configchanges to "Orientation|keyboardhidden|screensize",The screen does not recall the individual lifecycles, only the Onconfigurationchanged method is executedNote:since Android 3.2 (API), screen size has also started to change with the device's shift. So,in cases where the Minisdkversion and Targetsdkversion properties set in Androidmanifest.xml are greater than or equal to 13,

If you want to prevent the program from reloading activity at run time, in addition to setting "orientation", you must also set "ScreenSize".

4.2, if there is a problem, you can add a "layoutdirection" test.

Summary: In fact, online about the activity life cycle of a lot of knowledge, but most of the content is either incomplete, or outdated, now based on the content, do a part of the correction, I hope you can learn, welcome to comment on the shooting brick

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Activity life cycle-the latest experiment-based summary

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.