FragmentTransaction hide/show doesn’t work sometimes

You need to reuse the same instance of a fragment that you wanted to hide or show. private fun replaceFragment(fragment: Fragment) { supportFragmentManager.beginTransaction().apply { if (fragment.isAdded) { show(fragment) } else { add(R.id.fmFragmentContainer, fragment) } supportFragmentManager.fragments.forEach { if (it != fragment && it.isAdded) { hide(it) } } }.commit() }

What does FragmentManager and FragmentTransaction exactly do?

getFragmentManager() Return the FragmentManager for interacting with fragments associated with this activity. FragmentManager which is used to create transactions for adding, removing or replacing fragments. fragmentManager.beginTransaction(); Start a series of edit operations on the Fragments associated with this FragmentManager. The FragmentTransaction object which will be used. fragmentTransaction.replace(R.id.fragment_container, mFeedFragment); Replaces the current fragment with the mFeedFragment … Read more

Fragment must be a public static class to be properly recreated from instance state

The error is not especially weird. If you were not getting this error before, that was weird. Android destroys and recreates fragments as part of a configuration change (e.g., screen rotation) and as part of rebuilding a task if needed (e.g., user switches to another app, your app’s process is terminated while it is in … Read more

Android FragmentManager BackStackRecord.run throwing NullPointerException

Answering my own question: This exception is (eventually) thrown when you call FragmentTransaction.remove(null); and FragmentTransaction.commit(); EDIT: And also, like Twice Circled and shinyuX point out in the comment; when calling the show(null) or add(null), attach(null) and detach(null) methods, and probably also hide(null) After calling commit(), the transaction will be queued in the FragmentManager. As a … Read more