我有以下工作代码:
if (bottomViewState.addToBackStack) {
fragmentManager!!.beginTransaction()
.replace(R.id.bottomNavHostFragment, bottomViewState.fragment, bottomViewState.tag.toString())
.addToBackStack(null)
.commit()
} else {
fragmentManager!!.beginTransaction()
.replace(R.id.bottomNavHostFragment, bottomViewState.fragment, bottomViewState.tag.toString())
.commit()
}
}
我将其用于底部导航,这样我可以从详细信息片段导航回到其父级,但是我不在后退堆栈底部栏的项目之间添加事务。当我尝试将此代码重构为以下代码时,会出现我的问题:
val transaction = fragmentManager!!.beginTransaction()
transaction.replace(R.id.bottomNavHostFragment, bottomViewState.fragment, bottomViewState.tag.toString())
if(bottomViewState.addToBackStack) {
transaction.addToBackStack(null)
}
transaction.commit()
此代码的问题是,我的片段从不显示,就像未提交事务一样。我在这里做什么错了?
答案 0 :(得分:0)
尝试一下
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.layout, new MyFragment()).addToBackStack(null).commit();