我希望能够从Compose导航到一个Fragment。我已经在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()
}
)
}
}
简单的片段:
@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>
从compose到Fragment是否可以导航?