Android-activity life cycle

Source: Internet
Author: User

The main references in this paper this article and Ningyugang, "Android Development art Exploration," a book, thanks to the two authors!

the classic activity life cycle diagram(image from the network):7 Methods A simple descriptive narration are for example the following:1.onCreate:Activitycalled when it is created2, Onstart:activity is being started, has been visible, but not yet out of the front desk. You can't interact with the user yet3.Onresume:activity is already visible. And out of the foreground, able to interact with the user4.OnPause:activity is stopping. Can do some storage data, stop animation, etc., but not too time-consuming. BecauseOnPause must run out first, the newActivity'sOnresume will not run5.OnStop: Same can't dotoo time-consuming operations6.ondestory:called when activity is destroyed7.Onrestart:activity is being started again, under normal circumstances. Whenwhen activity is never visible and becomes visible again,The Onrestart method will be calledBelow is a detailed scenario where 7 methods are called:A, for the first time enter:B, press home key, lock screen key and jump when:C, in particular, when jumping to a new activity, it is assumed that the new activity adopts transparent themes. So. The current activity does not callback the OnStop method:make a brief summary of the above log display:first, OnStart andOnresumeMethod,OnPause andThe OnStop method is almost identical from the descriptive narrative, in the ordinary case, there is no obvious difference,they always come in pairs, andThe OnStop method is always followedOnPause get run, but in some cases, just runOnPause and not runOnStop. For example, the top C, and this can be seenOnPause andsome differences in OnStop (OnStart andOnresume). Second,the Onsaveinstancestate method is called before OnStop. But it andOnPause who was first called, has not been found based.

In the article mentioned at the beginning of this article, the author's test showsOnsaveinstancestate Run first. In this paper, the test results showrun after onsaveinstancestate. Third, in the above test. Only The Onsaveinstancestate method is called, and onrestoreinstancestatemethod has not been called. Usein the book "Android Development Art exploration". The system will onlythe activity is about to be destroyed and the opportunity to show it again is to callOnrestoreinstancestate method. such as:D, when the screen switch:in the event of an abnormally terminated activity, the system invokes theOnsaveinstancestate method to save the currentActivitythe state, whenOnce the activity is created again, the system will callonrestoreinstancestate, and putwhen activity is destroyedthe bundle objects saved by the Onsaveinstancestate method are passed to the same time as the parametersOnrestoreinstancestate andOnCreate method,When recovering data, selectOnrestoreinstancestate andOnCreate methods are all capable, but BothThere is a difference:once the Onrestoreinstancestate method is called, its number of parameters(Bundle savedinstancestate) must have a value. However, in the case of normal startup,of the OnCreate method(Bundle savedinstancestate) The value is null, so assume that theOnCreate method for data recovery. Justit is necessary to infer first,Assumptionssavedinstancestate is not NULL, the descriptionActivitywas rebuilt. We will be able to retrieve the previously saved data for recovery.

in addition, in terms of time. the invocation of the Onrestoreinstancestate method is after OnStart. official documentation suggests using the onrestoreinstancestate method to recover data.

E. If you do not want activity to be created again when the screen is rotated, you can add orientation to the Configchanges property:screensize This option is more special, it is related to the compile option. This option does not cause activity to restart when both Minsdkversion and Targetsdkversion in the compilation options are below 13 o'clock. Doing so will cause the activity to restart. Additionally, the activity does not restart, despite the associated configuration. However, the system calls the Onconfigurationchanged method, which can be processed according to the requirements. F, specify the screen:The ability to set android:screenorientation= "Portrait" in the manifest file, or to use setrequestedorientation in a Java file (Activityinfo.screen_ orientation_portrait);specified as a vertical screen,set android:screenorientation= in the manifest file "Landscape". or use Setrequestedorientation (activityinfo.screen_orientation_landscape) in Java files;specifies a horizontal screen. in both cases, when the orientation of the screen changes, no matter what method is called.

G. Exit Activity:

Android-activity life cycle

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.