Comprehensive analysis of activity life cycle

Source: Internet
Author: User

Welcome to follow my GitHub and follow my csdn.

In Android applications, activity is the most important component and its life cycle (Lifecycle) is well known.
However, we need to pay attention to some details, in order to use the handy.

There are two main life cycle changes in Android, one is the user participates in the activity life cycle change, and the other is the change caused by system recycle or configuration modification.

The GitHub example in this article

Android Life cycle:

1. User participation

The two most common actions that users have, press the home key or the back key.
(1) Click the Home button, pause the current activity, call OnPause-onStop; Return again, restore the current page, call Onrestart, OnStart, Onresume.
(2) Click the Back button, close the current activity, call OnPause-OnStop, OnDestroy; Start again to rebuild and invoke from OnCreate.

OnStart () is activity visible and cannot be interacted with; Onresume () is activity visible and can be interacted with. OnPause is the activity cannot interact, must perform the previous activity's OnPause completes, the latter activity can start, in OnPause, cannot perform the complex operation , Doing so will affect the next activity startup speed. OnStop is activity off display.

Note: Starting a transparent page calls only OnPause and does not call OnStop, which is simply not interactive, but is still displayed in the foreground. Starts a non-transparent page and calls OnPause-onStop.

Perform actions, start transparent pages, close transparent pages, start non-transparent pages, and close non-transparent pages.

E/DEBUG-WCL:Mainactivity:onpauseE/DEBUG-WCL:mainactivity:onsaveinstancestateE/DEBUG-WCL:Translateactivity:onpauseE/DEBUG-WCL:Mainactivity:onresumeE/DEBUG-WCL:Translateactivity:onstopE/DEBUG-WCL:Translateactivity:ondestroyE/DEBUG-WCL:Mainactivity:onpauseE/DEBUG-WCL:secondactivity:oncreateE/DEBUG-WCL:mainactivity:onsaveinstancestateE/DEBUG-WCL:Mainactivity:onstopE/DEBUG-WCL:Secondactivity:onpauseE/DEBUG-WCL:Mainactivity:onrestartE/DEBUG-WCL:Mainactivity:onstartE/DEBUG-WCL:Mainactivity:onresumeE/DEBUG-WCL:Secondactivity:onstopE/DEBUG-WCL:Secondactivity:ondestroy

Notice that the transparent page starts, and the home page only calls OnPause, not onstop.
The Onsaveinstancestate method, when closed, executes, but not the system call starts, and does not perform the operation of the recovery data, the Onrestoreinstancestate method.

2. System rebuild

When the system configuration changes, the activity will be rebuilt, because it is System control shutdown and start, will call Onsaveinstancestate save data, onrestoreinstancestate recover data.

E/DEBUG-WCL : mainactivity:onpause  E/DEBUG-WCL : mainactivity:onsaveinstancestate  E/DEBUG-WCL : mainactivity:onstop  E/DEBUG-WCL : mainactivity:ondestroy  E/DEBUG-WCL : mainactivity:oncreate  E/DEBUG-WCL : mainactivity:onstart  E/DEBUG-WCL : mainactivity:onrestoreinstancestate  E/DEBUG-WCL : mainactivity:onresume   

The onrestoreinstancestate executes before it can interact (Onresume), onsaveinstancestate after stopping the interaction (onPause).

The system saves the view structure and staging data before closing. View-specific data items that can be saved by reading the document or reading the code directly. The Onsaveinstancestate method of the Search view class, the class savedstate is the saved data.

TextView's Onsaveinstancestate

    @Override     PublicParcelableonsaveinstancestate() {Parcelable superstate =Super. Onsaveinstancestate ();//Save state if we is forced to        BooleanSave = Mfreezestext;intStart =0;intEnd =0;if(MText! =NULL) {start = Getselectionstart (); End = Getselectionend ();if(Start >=0|| End >=0) {//or save state if there is a selectionSave =true; }        }if(save) {savedstate SS =NewSavedstate (superstate);//XXX should also save the current scroll position!Ss.selstart = start; Ss.selend = end;if(MTextinstanceofspanned) {Spannable SP =NewSpannablestringbuilder (MText);if(Meditor! =NULL) {Removemisspelledspans (SP);                Sp.removespan (Meditor.msuggestionrangespan);            } ss.text = SP; }Else{ss.text = mtext.tostring (); }if(isFocused () && start >=0&& End >=0) {Ss.frozenwithfocus =true; } Ss.error = GetError ();if(Meditor! =NULL) {ss.editorstate = Meditor.saveinstancestate (); }returnss }returnSuperstate; }

TextView mainly preserves the text and focus of the content, as well as some location and error messages.

If the recovery of custom data, recovery time, you can choose OnCreate or onrestoreinstancestate, the difference is oncreate need to determine whether savedinstancestate is empty, The Onrestoreinstancestate parameter savedinstancestate is definitely non-null, otherwise it will not be called. It is recommended to use Onrestoreinstancestate.

OnCreate sentenced to empty

        if!=null) {            String= savedInstanceState.getString(EXTRA_TEXT);            Log.e(TAG"[onCreate]savedInstanceState: "+ txt);        }

In the activity property of Androidmanifest, add

android:configChanges="orientation|screenSize"

Prevents the page from redrawing while the screen is rotated, but the screen can still be rotated .
Instead of calling the life cycle, call onconfigurationchanged to handle rotating screen events.

Using android:screenorientation= "Portrait" keeps the screen upright and cannot be rotated.

Animation

Reference

OK, that ' s all! Enjoy it!

Comprehensive analysis of 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.