Sunday 4 October 2020

IllegalStateException Fragment already added

I am getting a non-fatal exception on firebase Crashlytics:

Non-fatal Exception: java.lang.IllegalStateException
Fragment already added

Here is my related code:

 var isFragmentAdded = false
        for (supportFragment in supportFragmentManager.fragments) {
            if (supportFragment::class == fragment::class) isFragmentAdded = true
        }
        if (isFragmentAdded || fragment.isAdded) {
            transaction.show(fragment)
        } else {
            transaction.add(R.id.contentFragment, fragment)
        }
        transaction.commitAllowingStateLoss()

As you can see, I check if the fragment was added in two ways before add the fragment. Why does this issue appear? (I was not be able to reproduce it on my devices or emulators)



from IllegalStateException Fragment already added

No comments:

Post a Comment