andrewedgar
andrewedgar

Reputation: 877

Clear Fragment Backstack immediately after transaction

I have an Activity with a cancel button that sends people back to a a starting Fragment:

cancelButton.setOnClickListener{
            supportFragmentManager.beginTransaction().replace(R.id.fragment_holder,StartFragment.newInstance()).commit()
    }

I would like to clear the entire Fragment backstack when this starting Fragment is returned to. I am attempting to do this in the Fragment's onStart method:

 while (activity!!.supportFragmentManager!!.backStackEntryCount > 0) {
            activity?.supportFragmentManager?.popBackStackImmediate()
        }

Doing this throws the common java.lang.IllegalStateException: FragmentManager is already executing transactions error when the cancel button is pressed, but even after reading about other people's problems, I still can't figure out how these transactions work and what is happening to cause this error. The fragments are contained within a FrameLayout.

Upvotes: 0

Views: 62

Answers (1)

Viraj S
Viraj S

Reputation: 400

after replace and before commit, do the pop operation and it should work.

Upvotes: 1

Related Questions