【问题标题】:Fragments Resume: Show and hide transactions don't work片段简历:显示和隐藏交易不起作用
【发布时间】:2019-02-23 04:49:30
【问题描述】:

我想恢复后台堆栈中的 android 片段。在底部导航的不同选项卡之间切换时,我不希望重新创建片段的视图。

我阅读了thisthisthis 以及有关 stackoverflow 的其他一些与此相关的问题。常见的建议是使用片段交易的显示和隐藏方法,但它不起作用。这是我的 kotlin 代码:

    bottomnavigation.setOnNavigationItemSelectedListener {
        item ->
        when(item.itemId){
            R.id.first_fragment_item -> {
                var fragment:Fragment = FirstFragment.newInstance()
                replaceFragment(fragment)
                return@setOnNavigationItemSelectedListener true
            }
            R.id.second_fragment_item -> {
                var fragment:Fragment = SecondFragment.newInstance()
                replaceFragment(fragment)
                return@setOnNavigationItemSelectedListener true
            }

        }
        return@setOnNavigationItemSelectedListener false

    }

}

  fun replaceFragment(fragment:Fragment) {

    var fragmentName:String = fragment::class.simpleName!!

    if(supportFragmentManager.findFragmentByTag(fragmentName)==null) {
        val fragmentTransaction = supportFragmentManager.beginTransaction()
        fragmentTransaction.replace(R.id.fragment_container, fragment, fragmentName)
        fragmentTransaction.addToBackStack(fragmentName)
        fragmentTransaction.commit()
    }
    else{
        if(fragmentName == "FirstFragment") {
            supportFragmentManager.beginTransaction().
                     hide(supportFragmentManager.findFragmentByTag("SecondFragment"))
                    .show(supportFragmentManager.findFragmentByTag("FirstFragment"))
                    .commit()
        }
        else{
            supportFragmentManager.beginTransaction()
                    .hide(supportFragmentManager.findFragmentByTag("FirstFragment"))
                    .show(supportFragmentManager.findFragmentByTag("SecondFragment"))
                    .commit()
        }
    }

}

总是显示 backstack 的最后一个片段。当我想隐藏它并显示时 另一个片段,那个片段屏幕变成白色,上面什么都没有。

我的底部导航有四个项目,但出于测试目的,我只使用了两个。其中两个片段是 FirstFragment 和 SecondFragment。我正在使用 v4.app.fragments。

【问题讨论】:

  • 使用.add()片段方法
  • @KishanViramgama add() 方法确实有效。但是当我使用它时,会覆盖不同片段的不同布局。请问有什么解决这个问题的建议吗?
  • 我通过为片段添加背景颜色解决了布局重叠问题。这是一个好的解决方案吗? @KishanViramgama
  • 我以前回答过类似的问题。我认为它可以帮助你:stackoverflow.com/questions/52172060/…

标签: android android-fragments kotlin


【解决方案1】:

Kishan Viramgama 评论的帮助下,我解决了我的问题。我使用“添加”方法而不是“替换”方法。这是我恢复片段的代码:`

fun replaceFragment(fragment: Fragment) {
    var nameOfFragment: String? = fragment::class.simpleName
    var fragmentTransaction = supportFragmentManager.beginTransaction()
    var fm: FragmentManager = supportFragmentManager
    if (supportFragmentManager.findFragmentByTag(nameOfFragment) == null) {
        fragmentTransaction.add(R.id.root_fragments, fragment, nameOfFragment)      //if fragment is not added to the backstack,add it/ don't use replace because it creates new fragment emptying fragment container
        fragmentTransaction.addToBackStack(nameOfFragment)
        fragmentTransaction.commit()
    } else {
        var fragmentToShow:Fragment = supportFragmentManager.findFragmentByTag(nameOfFragment) //if fragment is already in backstack,show it and hide all other fragments.
        for (i in 0 until fm.backStackEntryCount) {
            var fragmentToHide: Fragment = fm.findFragmentByTag(fm.getBackStackEntryAt(i).name)
            if (fragmentToHide::class.simpleName != fragment::class.simpleName)
                fragmentTransaction.hide(fragmentToHide)
        }
        fragmentTransaction.show(fragmentToShow)
        fragmentTransaction.commit()
    }

}

`

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-06
  • 1970-01-01
  • 2012-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多