Android Fragment UI not updated after popBackStack()

Rick_HK
1 min readAug 18, 2020

--

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

--

--

Rick_HK

Hi this is Rick from Hong Kong. I am a native iOS and Android mobile developer and also a tech enthusiast. Find me on Twitter https://twitter.com/rick3817