Search code examples
androidandroid-fragmentsandroid-architecture-navigationandroid-navigationandroid-jetpack-compose

Jetpack Compose NavHost with Fragments


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>(...) {
                ...
            }
  }

Solution

  • 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.