如何在撰写视图中膨胀的片段之间导航?



我正在用Compose重写一个Android应用程序,一切都很顺利。然而,在我的应用程序,我有一个调试屏幕,这是链接到一些片段调试。遗憾的是,要使用组合导航,所有组件都必须是可组合的……我现在不能在Compose中重写这些Fragments,所以我使用了这样的包装器屏幕:

@Composable
fun DeviceInfoScreen(navController : NavController, fragmentManager: FragmentManager) {
Scaffold(
topBar = {
TopAppBar(
title = {
Text(text = LocalContext.current.getString(R.string.debug_title))
},
navigationIcon = {
IconButton(onClick = {navController.navigateUp()}) {
Icon(Icons.Filled.ArrowBack, "backIcon")
}
},
backgroundColor = colorResource(id = R.color.primary),
contentColor = Color.White,
elevation = 77.dp)
},
content = {
Surface(color = colorResource(id = R.color.background), modifier = Modifier.padding(it)) {
AndroidViewBinding(FragmentDeviceInfoContainerBinding::inflate) {
val myFragment = fragmentDeviceInfoWrapper.getFragment<MyDeviceInfoFragment>()
}
}
})
}

我的布局是这样的:

<androidx.fragment.app.FragmentContainerView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragmentDeviceInfoWrapper"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:name="com.mypackage.MyDeviceInfoFragment">
</androidx.fragment.app.FragmentContainerView>

所以我的片段是正确加载的,但这个片段不能导航到多个其他片段,这也可以导航到其他片段后…是否有一个合适的方法来给一个NavController,或者一个新的活动外撰写,使这些片段可以链接在一起,是独立的?或者任何允许在片段之间导航的东西,同时仍然来自可组合的。

我尝试了一些东西,比如编辑片段中的代码,并使用这样的事务:

childFragmentManager.beginTransaction().replace(R.id.fragmentDeviceInfoWrapper, SecondFragment()).addToBackStack(null).commit()

但是我崩溃了:java.lang.IllegalArgumentException: No view found for id 0x7f0a031d (com.mypackage:id/fragmentDeviceInfoWrapper) for fragment SecondFragment{e343337}

我现在没有主意了,我想我没有在其他地方找到类似的东西,所以我希望你能帮助我!由于

尝试使用简单的<fragment>标签代替<androidx.fragment.app.FragmentContainerView>