0

I have several fragments in one activity. I want to save state of fragments when I switching between them. But activity don't recreated and onSaveInstanceState don't call. Can I somehow save bundle, for example, in onPause ?

sandeor30r
  • 25
  • 5
  • Possible duplicate of [Once for all, how to correctly save instance state of Fragments in back stack?](https://stackoverflow.com/questions/15313598/once-for-all-how-to-correctly-save-instance-state-of-fragments-in-back-stack) – Bö macht Blau Sep 26 '17 at 16:48

1 Answers1

0

Use the method addToBackStack() like this each time you create a new fragment.

if (fragment == null) {
        fragment = new MyFragmentClass();
        manager.beginTransaction()
                .add(R.id.your_fragment_id, fragment)
                .addToBackStack(null)
                .commit();
    } 

The fragment will be remembered after it is committed, and will reverse its operation when later popped off the stack.

Excel r 8
  • 105
  • 6