0

My application has a ‍‍‍‍BottomNavigation with 4 tabs, I had a fragment for every tab and add all of this 4 fragment to FragmentManager. When one of this tab is selected, I hide other fragment and show the fragment of this tab.
My problem is, when the screen is rotated, hidden fragments don't lose the state, and showing tab loose the state.
I try to hide the showing fragment in onSaveInstanceState and show it in onCreateView but it didn't work!
Why the hidden fragment saves their own state but showing fragment lost it? And how I can use this feature to save state of showing fragment?

My application using nested fragments, I have 4 main fragment and showing another page in that fragment I replace it with other fragment and save replaced fragment to backstack of childFragmentManager for saving backstack of every tab.
When I rotate the screen, only showing tab going back to first fragment like first time this tab is show, but other tabs saving their stats! I want to save the state of showing fragment like others, how can I do it?

Aswin P Ashok
  • 702
  • 8
  • 27
majidkabir
  • 132
  • 11
  • You have single container right?? Can you post your code – Mohammed Rampurawala Sep 16 '17 at 07:01
  • Yes, I have a single container, a `FrameLayout` for all of the fragments. I want to know what the difference between hiding fragments and showing fragments, and how I can use this feature for my showing fragment? – majidkabir Sep 16 '17 at 07:10
  • Try calling setRetainInstance(true) in your fragments. https://stackoverflow.com/questions/11182180/understanding-fragments-setretaininstanceboolean – mudit_sen Sep 16 '17 at 07:14
  • I use `setRetainInstance(true)` in `onCreateView` of fragment, but it doe's not work! – majidkabir Sep 16 '17 at 07:20
  • Rather than hiding fragment y dont you replace it ? – Mohammed Rampurawala Sep 16 '17 at 07:42
  • `FragmentTransaction transaction = fm.beginTransaction(); transaction.hide(oldFragment); transaction.show(newFragment); transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); transaction.commit(); ` this is my code for navigating between tabs – majidkabir Sep 16 '17 at 07:53

0 Answers0