los*_*ion 5 android android-navigation android-architecture-navigation android-jetpack-compose
我想从撰写导航到片段。我在 compose 中定义了一个 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()
}
)
}
}
Run Code Online (Sandbox Code Playgroud)
简单片段:
@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
}
}
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试导航时,出现以下异常:
java.lang.IllegalArgumentException: Navigation action/destination com.test/screenOne cannot be found from the current destination Destination(0x2b264dff) route=ScreenOne
Run Code Online (Sandbox Code Playgroud)
这是我的导航 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>
Run Code Online (Sandbox Code Playgroud)
是否可以从 compose 导航到 Fragment?
根据互操作性文档:
如果您想将导航组件与 Compose 一起使用,您有两个选择:
使用片段的导航组件定义导航图。
使用 Compose 目标在 Compose 中通过 NavHost 定义导航图。仅当导航图中的所有屏幕都是可组合的时,这才是可能的。
所以不,如果您使用 a ,则无法导航到片段目的地NavHost(反之亦然:如果您使用 a NavHostFragment,则无法导航到composable目的地:两个世界都不知道另一个世界)。
它实际上听起来像是您想要向 Compose 添加一个 Fragment。这将允许您的所有目的地都是composable您的单个 中的目的地NavHost,但让一个屏幕完全由一个片段(您的 )实现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>()
// ...
}
Run Code Online (Sandbox Code Playgroud)
}
| 归档时间: |
|
| 查看次数: |
5121 次 |
| 最近记录: |