Android performance optimization activity and fragment save and recover data via onsaveinstancestate ()

Source: Internet
Author: User

Both activity and fragment have their own life cycles, and they are very similar. Fragment is more than the activity Onattach () and Oncreateview () These methods. The whole of them is the same cycle that will go through from creating a View (OnCreate (), Oncreateview (), OnStart ()) To the Pause (OnPause (), OnStop ()) to re-return (Onresume ()) to the last Destruction (Ondetroyview (), OnDestroy ()) These methods.


As mentioned before, these lifecycles of familiarity with components are beneficial for us to preserve data at the appropriate time, and we can process and save the necessary data before the activity or fragment is cleaned up by the system or our own people, and it will be a good thing for users to be able to use the data again next time. ~


First look at a step, if it is artificially closed activity or fragment, such as click on the Back button, actively close the current page, will go onPause ()--onStop ()--OnDestroy () and  Onsaveinstancestate () will not be called. Onsaveinstancestate () is only called if the system is about to automatically clean up the activity or fragment before it is destroyed, such as

1, because the gravity sensor phone from the vertical screen into a horizontal screen,

2, mobile Phone Click home button and long Press home button

3. When you click on the Power Key lock screen

4, jumping from the current activity to another activity

5, the application of low memory will be automatically destroyed when the situation


Combined with the above, it can be seen that the onsaveinstancestate () method is suitable for temporary preservation of some non-permanent data. If you want to persist the data, put the action in OnStop (), OnDestroy (). onsaveinstancestate () is suitable for the current activity or fragment once destroyed by the system itself, the application can also save some necessary data before this, and the user's operation quickly back to the current page, At this point the data is not lost and can be restored to the maximum extent possible. This is the biggest meaning of the method. The example below is a simple example:

In the Fragment

The system automatically destroys fragment before saving the necessary data @overridepublic void Onsaveinstancestate (Bundle outstate) {<span style= "White-space:pre" ></span>super.onsaveinstancestate (outstate); <span style= "White-space:pre" ></span>String Content = Etcon.gettext (). toString (); <span style= "White-space:pre" ></span>outstate.putstring (" Inputcon ", content);} Recover data @overridepublic void onviewstaterestored (Bundle savedinstancestate) {<span style= "White-space:pre" >< /span>super.onviewstaterestored (savedinstancestate); <span style= "White-space:pre" ></span>// Restore the contents of the input box before <span style= "White-space:pre" ></span>if (savedinstancestate! = null) {<span style= " White-space:pre "></span>etcon.settext (savedinstancestate.getstring (" Inputcon "," "));}}


In the activity

Save the necessary data before the system automatically destroys the activity @overridepublic void Onsaveinstancestate (Bundle outstate) {<span style= "White-space:pre" ></span>super.onsaveinstancestate (outstate); String content = Etcon.gettext (). toString (); Outstate.putstring ("Inputcon", content);} The activity can be recovered in the Onrestoreinstancestate () method or in OnCreate (), because once the activity is destroyed, it will go back one time OnCreate () The start of the normal life cycle @overridepublic void Onrestoreinstancestate (Bundle savedinstancestate) {super.onrestoreinstancestate ( savedinstancestate);//restore the contents of the box before the IF (savedinstancestate! = null) {Etcon.settext (Savedinstancestate.getstring (" Inputcon "," "));}} @Overridepublic void OnDestroy () {Super.ondestroy ();//activity is destroyed before the last data can be saved, after the village there is no such shop.}

Here is an important point, above the above mentioned Onsaveinstancestate () method only when the activity or fragment determined to be automatically purged by the system, will be called, if not long-term background applications, the page is not killed by the system, The corresponding activity or fragment will not call Onsaveinstancestate () these methods.




Android performance optimization activity and fragment save and restore data through onsaveinstancestate ()

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.