Save Fragment state?

1

In a simple app I have Fragments and need to save their status ,

So when the user returns from one fragment to another,

is there still text typed, number chosen, check box marked, etc ... suggestions?

    
asked by anonymous 28.06.2016 / 10:49

1 answer

1
  • In fragment, saves state overriding onSaveInstanceState() and saves in onActivityCreated() :
  • @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        ...
        if (savedInstanceState != null) {
            //Restore the fragment's state here
        }
    }
    
    ...
    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    
        //Save the fragment's state here
    }
    
  • In the activity, you have to save the instance fragment in onSaveInstanceState() and saves it in the onCreate() .
  • public void onCreate(Bundle savedInstanceState) {
        ...
        if (savedInstanceState != null) {
            //Restore the fragment's instance
            mContent = getSupportFragmentManager().getFragment(savedInstanceState, "mContent");
            ...
        }
        ...
    }
    
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    
        //Save the fragment's instance
        getSupportFragmentManager().putFragment(outState, "mContent", mContent);
    }
    

    I hope this helps.

    I wanted to comment, because respsta was already given in English OS, but as I could not have written here and traduzi.Espero this help.

    link

    The code is not in code, if anyone knows why, thank you!

        
    28.06.2016 / 11:01