Is it possible to mix fragment and composable dest...
# compose
l
Is it possible to mix fragment and composable destination in a
NavHost
? I want to add a fragment that I don’t own, from a library, as a destination. I tried:
Copy code
NavHost(
    navController = navController,
    startDestination = route,
) {
    composable(...) { ... )
    fragment<LibraryFragment>(id)
}
But when I run, the activity crashes with
Could not find Navigator with name "fragment". You must call NavController.addNavigator() for each navigation type.
If it’s not possible, is there another way to add a fragment to composition?
👍 2
i
This is not possible via Navigation Compose, no.
I don't know what the latest is on adding a fragment within a Compose hierarchy is (to be somewhat honest, I'd be surprised if it works consistently), but I would imagine it would involve an
AndroidView
or
AndroidViewBindin
(if you want to inflate a
FragmentContainerView
with an
android:name
) ala: https://developer.android.com/jetpack/compose/interop#views-in-compose
🤔 1
l
I’ll try the view binding approach. Thanks for your hindsight
i
If you look at apps like JetChat, it generally uses fragments as the outer layer in a hybrid app, but with each screen (i.e., each fragment) implementing its UI in Compose, rather than implementing everything in Compose then adding fragments, etc. within that hierarchy
Definitely do file bugs against Compose if you run into issues with that approach though - ideally, both approaches work
a
I tried AndroidView with a Fragment before, there’s still a crash when you’re using hardware keyboard in an emulator though it got fixed recently https://issuetracker.google.com/issues/165034731 Will be available in the next release
301 Views