I have three fragments A, B and C. And I'm using navHostFragment
container in MainActivity
. So the application goes from A -> B using kotlin extension function findNavController().navigate...
and then go from B to C using same function. All works fine till here.
Now in Fragment C, I'm replacing different elements on fragment C using
activity?.supportFragmentManager
?.beginTransaction()
?.replace(R.id.list_container, someFragment)
?.addToBackStack("some_frag_id")
?.commit()
The list_container
is replaced with someFragment
. After this when I press physical back button Fragment C pops out and my app goes to Fragment B while what I expect it to restore replaced list_container
i.e. whatever was there before replacement.
I'm also overiding this in my MainActivity
override fun onBackPressed() {
val count = supportFragmentManager.backStackEntryCount
if (count == 0) {
super.onBackPressed()
//additional code
}
else {
supportFragmentManager.popBackStack()
}
}
I'm not sure what is missing here. I have read a lot of solutions on stackoverflow but none worked to my satisfaction. Please guide.