Jetpack Compose NavHost with Fragments

Viewed 1877

Is it possible to use fragment destination inside the compose NavHost? I tried to create destinations with NavGraphBuilder DSL but I am getting the following error: Could not find Navigator with name "fragment". You must call NavController.addNavigator() for each navigation type.

Here is what I'm trying to do:

  NavHost(navController = navController, startDestination = NavScreen.Home.route) {
            composable(NavScreen.Home.route) {
               ...
            }
            
            fragment<TestFragment>(...) {
                ...
            }
  }
1 Answers

In alpha05 I don't think compose navigation is interoperable with fragment navigation. But you could wrap your Fragment in an AndroidView which is composes way of interoping with traditional android. Something like:

@Composable
fun MyFragDestination() {
    AndroidView(
        viewBlock = { // create fragment },
        updateBlock = { // update fragment with new state }
    }
}

And then call MyFragDestination from the compose navigator.

Related