Jorge Domínguez
10/29/2025, 4:34 PMAndroidViewBinding when hosting different fragments across two screens, hopefully someone here will be able to help me.
In Screen A, I host FragmentA, and in Screen B, I host FragmentB, both are instances of the same fragment class. Even though I use the same composable function, each one is hosted in a different FragmentContainerView (I handle this by passing a bindingFactory lambda to AndroidViewBinding). When navigating from Screen A → Screen B, everything works fine: FragmentManager properly removes FragmentA before showing FragmentB. However, when navigating back from Screen B → Screen A, their lifecycles seem to overlap: `AndroidViewBinding`’s onRelease from Screen B is called after the factory from Screen A. This ends up replacing FragmentA with FragmentB on Screen A 🧵Jorge Domínguez
10/29/2025, 4:34 PMFragmentB isn’t removed in time, so for a moment the FragmentManager holds both fragments, and for some reason it gets confused and removes FragmentA instead of FragmentB.Jorge Domínguez
10/29/2025, 4:35 PMAndroidViewBinding code to customize the onRelease logic, but even after adding a check to prevent the fragment removal when fragmentManager.fragmentCount > 1, FragmentA still gets removed.Jorge Domínguez
10/29/2025, 4:36 PMIan Lake
10/29/2025, 4:41 PMAndroidViewBinding for fragments anymore - use AndroidFragment which was built specifically because of these unsolvable problems with AndroidViewBindingIan Lake
10/29/2025, 4:42 PMJorge Domínguez
10/29/2025, 4:50 PMAndroidFragment before, but I didn’t see a way to use it when having an existing instance of the fragment, which is what my current setup requires because I get the fragment instance from an SDK. Is there a way to use it like that?Ian Lake
10/29/2025, 5:10 PMAndroidFragment uses the FragmentFactory you've set on your FragmentManager to create instances of your Fragment since that is what is going to unconditionally be used after you go through a config change / process death+recreation anywaysIan Lake
10/29/2025, 5:14 PMfragment.arguments and set those on AndroidFragment) or they're already not handling config changes correctly in the first placeJorge Domínguez
10/29/2025, 5:38 PMAndroidFragment. Interestingly, the problem still happens: FragmentA is replaced by FragmentB, but only the first time I navigate from Screen B → Screen A. After that, if I reset the state on Screen A so that FragmentA is shown again, it no longer occurs. I’m using the Navigation Component, and both screens are separate destinations in a bottom nav bar, could that be related?Ian Lake
10/29/2025, 5:45 PMcurrentCompositeKeyHash right before each AndroidFragment call? Do you get different values or the same?Jorge Domínguez
10/29/2025, 8:08 PMIan Lake
10/29/2025, 8:37 PMAndroidFragment is putting the fragments in different containers and there isn't any overlap between themJorge Domínguez
10/29/2025, 8:50 PMAndroidViewBinding, when `AndroidFragment`’s onDispose is invoked the FragmentManager is removing FragmentA instead of FragmentB, even though they’re hosted by different containersJorge Domínguez
10/29/2025, 8:50 PMAndroidFragment recomposition on Screen A seem to prevent that from happening.Ian Lake
10/29/2025, 8:51 PMIan Lake
10/29/2025, 8:52 PMAndroidFragment only looks up fragments by the container, which you said was different, so I'm not seeing how one could affect the otherIan Lake
10/29/2025, 8:53 PMJorge Domínguez
10/29/2025, 10:10 PM