从撰写到片段

发布于 2025-02-02 18:24:19 字数 1459 浏览 2 评论 0原文

我想从撰写到片段。我已经定义了一个NAVHOST:

NavHost(
   navController = navController,
   startDestination = DrawerScreen.Screen2.route
) {
   composable(DrawerScreen.Screen1.route) {
      navController.navigate(R.id.screen_one)
   }
   composable(DrawerScreen.Screen2.route) {
      Screen2(
         openDrawer = {
            openDrawer()
         }
      )
   }
}

简单片段:

@AndroidEntryPoint
class ScreenOneFragment: Fragment() {

   @Nullable
   override fun onCreateView(
      inflater: LayoutInflater,
      @Nullable container: ViewGroup?,
      @Nullable savedInstanceState: Bundle?
   ): View {
      val view: View = inflater.inflate(R.layout.screen_one, container, false)
      return view
   }
}

但是,当我尝试导航时,我会得到以下例外:

java.lang.IllegalArgumentException: Navigation action/destination com.test/screenOne cannot be found from the current destination Destination(0x2b264dff) route=ScreenOne

这是我的导航XML:

<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/mobile_navigation"
    app:startDestination="@id/screenOne">
    <fragment
        android:id="@+id/screenOne"
        android:name="com.test.ScreenOne"
        android:label="ScreenOne" />
</navigation>

是否可以从撰写到片段?

I would like to navigate from compose to a Fragment. I have defined a NavHost in compose:

NavHost(
   navController = navController,
   startDestination = DrawerScreen.Screen2.route
) {
   composable(DrawerScreen.Screen1.route) {
      navController.navigate(R.id.screen_one)
   }
   composable(DrawerScreen.Screen2.route) {
      Screen2(
         openDrawer = {
            openDrawer()
         }
      )
   }
}

Simple Fragment:

@AndroidEntryPoint
class ScreenOneFragment: Fragment() {

   @Nullable
   override fun onCreateView(
      inflater: LayoutInflater,
      @Nullable container: ViewGroup?,
      @Nullable savedInstanceState: Bundle?
   ): View {
      val view: View = inflater.inflate(R.layout.screen_one, container, false)
      return view
   }
}

However when I try to navigate I get the following exception:

java.lang.IllegalArgumentException: Navigation action/destination com.test/screenOne cannot be found from the current destination Destination(0x2b264dff) route=ScreenOne

Here is my navigation xml:

<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/mobile_navigation"
    app:startDestination="@id/screenOne">
    <fragment
        android:id="@+id/screenOne"
        android:name="com.test.ScreenOne"
        android:label="ScreenOne" />
</navigation>

Is it possible to navigate from compose to a Fragment?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一瞬间的火花 2025-02-09 18:24:19

根据互操作性文档

如果要使用Compose使用导航组件,则有两个选择:

  • 定义带有片段导航组件的导航图。

  • 定义带有NAVHOST的导航图,并使用组合目的地组成。 仅当导航图中的所有屏幕都是组合物中才有可能。

因此,当您使用navhost时,您不能导航到片段目标(反向是另外:如果您使用的是navhostfragment,则无法导航到composable目标>目标:彼此都不知道另一个世界)。

实际上听起来是您要添加一个片段撰写。这将使您的所有目的地都可以在您的单个nav -host中 composable 目的地,但是让一个屏幕完全由片段实现(您的ScreenOne )。

composable(DrawerScreen.Screen1.route) {
  // Assumes you have a layout with a FragmentContainerView
  // that uses android:name="com.test.ScreenOneFragment"
  AndroidViewBinding(ScreenOneLayoutBinding::inflate) {
    val myFragment = fragmentContainerView.getFragment<ScreenOneFragment>()
    // ...
}

}

As per the Interopability docs:

If you want to use the Navigation component with Compose, you have two options:

  • Define a navigation graph with the Navigation component for fragments.

  • Define a navigation graph with a NavHost in Compose using Compose destinations. This is possible only if all of the screens in the navigation graph are composables.

So no, you cannot navigate to a fragment destination if you are using a NavHost (the reverse is also true: if you are using a NavHostFragment, you can't navigate to a composable destination: neither world knows about the other).

What it actually sounds like is that you want to add a Fragment to Compose. This would allow all of your destinations to be composable destinations in your single NavHost, but let one screen be implemented entirely by a fragment (your ScreenOne).

composable(DrawerScreen.Screen1.route) {
  // Assumes you have a layout with a FragmentContainerView
  // that uses android:name="com.test.ScreenOneFragment"
  AndroidViewBinding(ScreenOneLayoutBinding::inflate) {
    val myFragment = fragmentContainerView.getFragment<ScreenOneFragment>()
    // ...
}

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文