How to save fragment state with Onsaveinstancestate in Android _android

Source: Internet
Author: User

Inside the fragment, use Onsaveinstancestate to save the data and restore the data in onactivitycreated.

public void onactivitycreated (Bundle savedinstancestate) {
  super.onactivitycreated (savedinstancestate);
  ...
  if (savedinstancestate!= null) {
    //Restore The fragment ' s here
  }} is public

void Onsaveinstancestat E (Bundle outstate) {
  super.onsaveinstancestate (outstate);
  Save The fragment ' s state here
}

Within the activity, use Onsaveinstancestate to save the fragment instance data and restore the data in OnCreate:

Private Fragment myfragment;

public void OnCreate (Bundle savedinstancestate) {
  ...
  if (savedinstancestate = null) {
    //instantiate fragment
  } else {
    //Restore The fragment ' s instance
    myfr Agment = Getsupportfragmentmanager (). Getfragment (
          savedinstancestate, "fragment");
  ...
}

@Override
protected void onsaveinstancestate (Bundle outstate) {
  super.onsaveinstancestate (outstate);
  Save The fragment ' s instance
  //Fragment instance may be null
  if (myfragment!= null) {
    getsupportfragmen Tmanager (). Putfragment (Outstate, "Fragment", myfragment);
  }


Instance
first look at a step, if it is artificially close the activity or fragment, such as clicking the Back button, active close the current page, will go OnPause ()--> onStop ()--> OnDestroy () and Onsaveinstancestate () is not invoked. Onsaveinstancestate () will only be invoked before the system is about to automatically clean up the activity or fragment, such as

1, since the gravity sensor is turned from a vertical screen to a horizontal screen,

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

3, click the power button to lock the screen

4, jump from current activity to another activity

5, the application of insufficient 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-persistent data. If you want to persist the data, put the action in OnStop (), OnDestroy () These methods. 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 returned to the current page, this time the data is not lost , and can recover to the maximum extent possible. This is the biggest meaning of this method. The following example is a simple example:

In the Fragment:

The system automatically destroys fragment before saving the necessary data
@Override public
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
@Override public
void onviewstaterestored (Bundle savedinstancestate) {
<span style= " White-space:pre "> </span>super.onviewstaterestored (savedinstancestate);
<span style= "White-space:pre" > </span>//the contents of the input box before the restore
<span style= "White-space:pre" > </span >if (savedinstancestate!= null) {
<span style= "White-space:pre" > </span>etcon.settext ( Savedinstancestate.getstring ("Inputcon", ""));
 }


In the activity

The system automatically destroys the activity before it saves the necessary data
@Override public
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 recover the data in the Onrestoreinstancestate () method, or it can be recovered in OnCreate (), because once the activity is destroyed it will go back to the OnCreate () the normal life cycle that begins
Override public
void Onrestoreinstancestate (Bundle savedinstancestate) {
 super.onrestoreinstancestate ( Savedinstancestate);
 The contents of the input box before the restore
 if (savedinstancestate!= null) {
 Etcon.settext (savedinstancestate.getstring ("Inputcon", "") );
 }
}
 
@Override public
void OnDestroy () {
 Super.ondestroy ();
 The last time the activity can be saved before it is destroyed, the store will not be available after this village.
}

Here is an important point where the Onsaveinstancestate () method mentioned above is invoked only when the activity or fragment is determined to be automatically cleared by the system, and if it is not a long-term background application, the page is not killed by the system. The corresponding activity or fragment does not invoke the Onsaveinstancestate () method.

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.