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