Piasy/SafelyAndroid

Misunderstood "id" parameter in several methods of FragmentTransactionBuilder

Closed this issue · 2 comments

It is containerViewId rather than the fragment itself's id. So there is no sense to call methods like checkIdNotExist(mFragmentManager, id).

See Details in https://developer.android.com/reference/android/app/FragmentTransaction.html#add(int, android.app.Fragment, java.lang.String).

Piasy commented

Actually, the id, or tag, is used for removing a fragment, we do this by finding a fragment with id or tag, what it means exactly is not important here.

If the id/tag doesn't exist, FragmentManager will return null, and if we do a transaction with null, NPE will be thrown, so we want make sure we could find a fragment with the id/tag, so we call checkIdExist/checkTagExist.

What if multiple fragment has the same container id/tag? Maybe that's not we really want to do, so I choose to enforce every id/tag could only be bound to one fragment(by calling checkIdNotExist/checkTagNotExist when add). If you really want to bind multiple fragments with the same id/tag, do a normal fragment transaction is ok. Anyway, add transaction won't throw exception. In case of doing normal fragment transaction, you still need safe commit, to avoid activity state loss.

Thanks for you reply. Clear now.