【发布时间】:2019-11-20 17:55:46
【问题描述】:
我在容器中更改片段时遇到问题。我有三个导航:Home、Special offers、Profile。在Home 导航中,它可能是fragment1_1 或fragment2_2,具体取决于具体情况。我的问题是从我的容器中显示片段。我尝试使用findFragmentById,但是当我在Profile 导航并尝试转到Home 时,我的代码不会来自hide() 片段Pofile。我试图查看日志,发现它隐藏了Home 并显示了Home。我的导航代码:
botNav.setOnNavigationItemSelectedListener {
when(it.itemId){
R.id.act_home -> {
if (!it.isChecked){
val homeFragment = supportFragmentManager.findFragmentByTag("Home")
activeFragment = supportFragmentManager.findFragmentById(R.id.fragment_container)
activeFragment?.let { hideFragment(it) }
showFragment(homeFragment!!)
}
}
R.id.act_profile_info -> {
if (!it.isChecked) {
activeFragment = supportFragmentManager.findFragmentById(R.id.fragment_container)
activeFragment?.let { hideFragment(it) }
showFragment(profileFragment)
}
}
R.id.act_special_offer -> {
if (!it.isChecked) {
activeFragment = supportFragmentManager.findFragmentById(R.id.fragment_container)
activeFragment?.let { hideFragment(it) }
showFragment(specialFragment)
}
}
}
return@setOnNavigationItemSelectedListener true
}
所以现在我想了解它为什么会这样,以及如何从FrameLayout 容器中获取显示的片段。
为了为我的导航添加片段,我使用了addFragment() 函数。
【问题讨论】:
-
添加你现在得到的图片。