简体   繁体   中英

Saving Fragment State methods

For saving fragment sate I think I came across two methods:

  1. Using onSaveInstanceState.
  2. Using Backstack to hide, add, and show the fragment.

I am unsure about whether these are two methods or both are required eg Is onSaveInstanceState specifically used to handle rotation change only or any one of them can be used?

You can save instance of fragment using onSaveInstanceState. No need of second method. However you want to recreate fragment on back preess then replace fragment instead of add fragment. If you want to refresh Fragment then call on Resume method and get refreshed data by calling your method at on Resume

@Override
    public void onBackStackChanged() {
        FragmentManager fragmentManager = getSupportFragmentManager();
        Fragment fr = fragmentManager.findFragmentById(R.id.content_frame);
        if (fr != null) {
            fr.onResume();
        }

    }

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