多个片段重叠且未隐藏在导航上

问题描述 投票:0回答:1

我的应用程序中有2个随机重复出现的问题,当前在生产环境中弹出:

  • 经过一段时间后恢复/重新打开应用程序时。在它们之间切换时,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时,每次切换导航都会调用创建。

任何帮助将不胜感激!

android android-layout android-fragments kotlin android-navigation
1个回答
0
投票

最终更改了实现,改用了this。对于任何在这个奇怪的问题上遇到麻烦并正在使用supportFragmentManager的人,请给这个机会。片段之间的navgraph使用navigation。看起来这也是推荐的方法。

© www.soinside.com 2019 - 2024. All rights reserved.