3

In my activity, I have a bottom navigation view with 5 items. On each item click in bottom navigation view, I am loading a fragment. Suppose I am on the 3rd item and I am changing the orientation, then activity was recreated and it showed up 1st item as selected. This I avoided by giving the following code in my activity.

override fun onSaveInstanceState(outState: Bundle) {
    super.onSaveInstanceState(outState)
    outState.clear()
    outState.putInt("tabSelected", bottomNav.selectedItemId)
}

override fun onRestoreInstanceState(savedInstanceState: Bundle) {
    super.onRestoreInstanceState(savedInstanceState)
    bottomNav.selectedItemId = savedInstanceState.getInt("tabSelected")
}

By giving the above code, I managed to show 3rd item as selected. But the issue here is the fragment is getting recreated and my savedInstanceState is always null in that fragment. I have given

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    retainInstance = true
}

in my fragment.

How can I save all the content of my fragment in this case?

UPDATE: I don't see any fragments in my supportFragmentManager although I have given retainInstance = true

Anju
  • 9,379
  • 14
  • 55
  • 94

3 Answers3

1

Make sure to write below line in manifest file for your activity in which you are loading fragment.

android:configChanges="orientation|screenSize|screenLayout|keyboardHidden"
Ankit Lathiya
  • 199
  • 1
  • 12
  • This can be used only if my layout is the same for both orientations. In my case, I have different layouts. – Anju Apr 06 '20 at 08:39
0

Have you tried adding the .addToBackStack(null) method to the transaction manager right when you replace the fragment with a new one. this should save the state of the fragments before you call commit to the savedinstancestate. if you do .addToBackStack(String tag) you can even call up the backstack of specific fragments.

here is some documentation

I think if you want to retain configurations in the fragments themselves you can also read up on setRetainInstance(true) here and here this method will avoid your fragment being recreated on device orientation but if you do thos you might need to override the methods onActivityCreated, onAttach and onDetach in your fragment and tell them how to draw your fragment instead of doing it in onCreateView as you are probably doing now.

quealegriamasalegre
  • 2,887
  • 1
  • 13
  • 35
0
bottomNav.selectedItemId = savedInstanceState.getInt("tabSelected")

This line reselects the item in the bottom navigation but it also triggers the menu action. It could be that the menu listener just replaces the existing fragment with a new instance.

The existing fragment should still be there with non-null savedINstanceState after device rotation

GSala
  • 946
  • 7
  • 15
  • Then how can I select and keep an item on orientation change then? – Anju Apr 01 '20 at 09:50
  • In the bottomNavigation listener, you can check if the fragment is already there, before replacing it – GSala Apr 01 '20 at 14:03
  • I have implemented in the same way. What I am not able to understand is why savedInstanceState is always null for me in my fragments? – Anju Apr 03 '20 at 13:06