多个片段重叠,在导航时不隐藏



我的应用程序中有两个随机出现的问题,目前在生产中弹出:

  • 一段时间后恢复/重新打开应用程序时。当在它们之间切换时,它具有彼此重叠的fragments
  • 当应用程序恢复/重新打开时,底部导航图标似乎不会对上下文切换做出响应,即使片段加载良好

上述两个问题都很难在本地重现。我已经尝试过替换碎片的解决方案。然而,在设备上,它们现在确实经常弹出。以下是我的MainActivity的样子:

class MainActivity : AppCompatActivity() {
private val TAG_FRAGMENT_HOME = "fragment_home"
private val TAG_FRAGMENT_NEWS = "fragment_news"
private val TAG_FRAGMENT_MARKET = "fragment_market"
private val TAG_FRAGMENT_EXPLORE = "fragment_explore"
private var bottomNavigationView: BottomNavigationView? = null
private lateinit var appBarConfiguration: AppBarConfiguration
private val homeFragment = HomeFragment()
private val marketsFragment = MarketsFragment()
private val newsFragment = NewsFragment()
private val exploreFragment = ExploreFragment()
private lateinit var active: Fragment
override fun onCreate(savedInstanceState: Bundle?) {
setTheme(R.style.AppTheme)
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
bottomNavigationView = findViewById(R.id.bottomNavigationView)
bottomNavigationView!!.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener)
}
private val mOnNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
when (item.itemId) {
// Set up navigation here
R.id.home -> {
supportFragmentManager.beginTransaction().hide(active).show(homeFragment).commit()
active = homeFragment
}
R.id.news ->{
supportFragmentManager.beginTransaction().hide(active).show(newsFragment).commit()
active = newsFragment
return@OnNavigationItemSelectedListener true
}
R.id.markets -> {
supportFragmentManager.beginTransaction().hide(active).show(marketsFragment).commit()
active = marketsFragment
return@OnNavigationItemSelectedListener true
}
R.id.explore -> {
supportFragmentManager.beginTransaction().hide(active).show(exploreFragment).commit()
active = exploreFragment
return@OnNavigationItemSelectedListener true
}
}
false
}
override fun onResume() {
super.onResume()
active = homeFragment
supportFragmentManager.beginTransaction()
if (!fragmentExists(TAG_FRAGMENT_EXPLORE)) {
supportFragmentManager.beginTransaction().add(R.id.main_container, exploreFragment, TAG_FRAGMENT_EXPLORE).hide(exploreFragment).commit()
}
if (!fragmentExists(TAG_FRAGMENT_MARKET)) {
supportFragmentManager.beginTransaction().add(R.id.main_container, marketsFragment, TAG_FRAGMENT_MARKET).hide(marketsFragment).commit()
}
if (!fragmentExists(TAG_FRAGMENT_NEWS)) {
supportFragmentManager.beginTransaction().add(R.id.main_container, newsFragment, TAG_FRAGMENT_NEWS).hide(newsFragment).commit()
}
if (!fragmentExists(TAG_FRAGMENT_HOME)) {
supportFragmentManager.beginTransaction().add(R.id.main_container,homeFragment, TAG_FRAGMENT_HOME).commit()
}
}
}

假设当用户重新打开应用程序时,调用onResume,我将尝试只添加那些尚未添加的片段。此外,我正在使用add而不是replace,因为我想要热切加载而不是懒惰加载。正如我所观察到的,使用replace fragment时,每次切换导航时都会调用创建。

任何帮助都将不胜感激!

最终更改了实现并使用了它。对于任何偶然发现这个奇怪问题并正在使用supportFragmentManager的人,请给这个机会。它使用navgraph作为片段之间的navigation。看起来这也是推荐的方法。

由于重新启动botNav菜单片段和一些额外的导航行为的问题,当我覆盖底部导航onItemSelectedonItemReselected方法时遇到了错误。因此,无论您的情况如何,请尝试删除被覆盖的onItemSelected,或者小心使用popBackStacknavigate,在onItemReselected中使用navController.popBackStack(itemId, false)

这对我有帮助,也可能对你有帮助。

谢谢。

最新更新