Have you ever experience your fragment’s UI not updating after exit from another fragment?
I spent hours to find out that the reason of this weirdness is that the fragment state after supportFragmentManager.popBackStack() is causing this issue
supportFragmentManager.popBackStack()
So, the solution is to use
supportFragmentManager.popBackStackImmediate("theFragment", 0)
instead, where the “theFragment” is the fragment name used when you add the fragment to back stack:
val transaction = supportFragmentManager.beginTransaction()
transaction.replace(R.id.fragment, fragment)
transaction.addToBackStack("theFragment")
transaction.commit()
Using supportFragmentManager.popBackStackImmediate(“theFragment”, 0) can make sure your fragment is in the correct state for updating the UI elements.
That’s it.
Happy coding.
Let me know if you find better solution or approach in the comment section below. ;)
Find me at Twitter @rick3817
Reference:
#android #androiddev #androidfragment #fragment #backstack