从撰写到片段
我想从撰写到片段。我已经定义了一个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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据互操作性文档:
因此,当您使用
navhost
时,您不能导航到片段目标(反向是另外:如果您使用的是navhostfragment
,则无法导航到composable
目标>目标:彼此都不知道另一个世界)。实际上听起来是您要添加一个片段撰写。这将使您的所有目的地都可以在您的单个
nav -host
中 composable 目的地,但是让一个屏幕完全由片段实现(您的ScreenOne
)。}
As per the Interopability docs:
So no, you cannot navigate to a fragment destination if you are using a
NavHost
(the reverse is also true: if you are using aNavHostFragment
, you can't navigate to acomposable
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 singleNavHost
, but let one screen be implemented entirely by a fragment (yourScreenOne
).}