简体   繁体   English

Jetpack 使用 Fragments 编写 NavHost

[英]Jetpack Compose NavHost with Fragments

Is it possible to use fragment destination inside the compose NavHost?是否可以在 compose NavHost 中使用片段目标? I tried to create destinations with NavGraphBuilder DSL but I am getting the following error: Could not find Navigator with name "fragment".我尝试使用 NavGraphBuilder DSL 创建目的地,但出现以下错误:找不到名称为“片段”的导航器。 You must call NavController.addNavigator() for each navigation type.您必须为每种导航类型调用 NavController.addNavigator()。

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.在 alpha05 中,我认为撰写导航不能与片段导航互操作。 But you could wrap your Fragment in an AndroidView which is composes way of interoping with traditional android.但是你可以将你的 Fragment 包装在一个AndroidView中,它构成了与传统 android 互操作的方式。 Something like:就像是:

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

And then call MyFragDestination from the compose navigator.然后从撰写导航器中调用MyFragDestination

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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