https://kotlinlang.org logo
l

Luke

11/20/2020, 6:43 PM
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

Ian Lake

11/20/2020, 7:20 PM
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

Luke

11/20/2020, 7:26 PM
I’ll try the view binding approach. Thanks for your hindsight
i

Ian Lake

11/20/2020, 7:29 PM
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

allan.conda

11/21/2020, 3:28 AM
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
88 Views