简体   繁体   中英

How to use onSaveInstanceState() for Fragments

I need to save some member variables when my Fragment is destroyed. However, research is showing me that Fragment variables are not saved onSaveInstanceState as for Activities. Therefore, I'm having trouble saving these variables.

My fragments are part of a PageViewer, so I can't figure out how to store the Fragments in the Bundle of the Activity's onSaveInstanceState (I don't have the Fragments defined explicitly there because they're created in the PageViewer Adapter!)

I've seen it suggested that I use Fragment Arguments to store these variables, but when I call containsKey on my Arguments bundle, I always get false .

What are your suggestions for saving member variables of Fragments when they their onStop is called?

This is my Fragment's onCreateView

public FinalCalcFragment(){
        setArguments(new Bundle());
    }    
@Override
    public View onCreateView(LayoutInflater inflater,
            ViewGroup container, Bundle savedInstanceState) {
        rootView = inflater.inflate(
                R.layout.activity_grade_calculator, container, false);
        Bundle args = getArguments();
        if(args.containsKey("visYes")){
            firstLoad = args.getBoolean(PERSISTENT_FIRSTLOAD_BUNDLE_KEY);
            System.out.println("contains the key");
        }
        context = this.getActivity();

        if(firstLoad) {   //This is so that I don't reset my views when recreating.
            bindAll();
            setInitialViews();  
            setListeners();
        }
        firstLoad = false;
        return rootView;
    }

Here are my onPause and onSaveInstanceState . I tried storing my arguments in both, but neither worked.

@Override
public void onPause(){
    super.onPause();
    getArguments().putBoolean(PERSISTENT_FIRSTLOAD_BUNDLE_KEY, firstLoad);
    getArguments().putBoolean("visYes", ((TextView) rootView.findViewById(R.id.tv_grade_calc_result)).isShown());
}

@Override
public void onSaveInstanceState(Bundle outState){
    super.onSaveInstanceState(outState);
    getArguments().putBoolean(PERSISTENT_FIRSTLOAD_BUNDLE_KEY, firstLoad);
    getArguments().putBoolean("visYes", ((TextView) rootView.findViewById(R.id.tv_grade_calc_result)).isShown());
}

And here's an excerpt from my adapter's getItem method.

@Override
public Fragment getItem(int i) {
    //System.out.println("We are trying to get item " + i);
    switch(i) {
        case 0:
            Fragment fragment = new FinalCalcFragment();
            return fragment;

You should use Bundle outState to save fragment instance state, just as with activities. Changing arguments returned by getArguments() has no effect. Then in onCreateView first check passed savedInstanceState. If savedInstanceState is not null use it, if it is null use Bundle returned by getArguments().

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM