我想获得与这段代码相同的结果,因为我正在使用 android 导航组件,我应该用什么来代替 fragment 容器,因为只有 NavHostFragment。
val newFragment = ExampleFragment()
val transaction = supportFragmentManager.beginTransaction()
transaction.replace(R.id.fragment_container, newFragment)
transaction.addToBackStack(null)
transaction.commit()
最佳答案
假设您的导航图中有一个目的地,如下所示:
<fragment
android:id="@+id/example"
android:name=".ExampleFragment"/>
您只需调用 navController.navigate(R.id.example)
,通过 the documentation 中描述的方法之一获取对您的 NavController
的引用.
关于android架构组件: What is the alternative to fragment container in android architecture navigation?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53855834/