简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM