避免碎片重现BottomNavigationView

问题描述

我想通过使用BottomNavigationView避免片段重新生成 我读到有关FragmentTransaction.replace的问题,更改添加会有所帮助,但这对我没有用。.

也许您可以在这里看到我错了

这是我的主持人活动,该活动通过BottomNavigationView托管3个片段

'''

class Host : AppCompatActivity() {




var fragment: Fragment? = null

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_host)

    //for hide the actionBar (contains Fragment name) on top of the screen
    val actionBar = supportActionBar
    actionBar?.hide()


    val navController = Navigation.findNavController(this,R.id.nav_host_fragment)
    val navView = findViewById<BottomNavigationView>(R.id.nav_view)
    navView?.setupWithNavController(navController)


    NavigationUI.setupWithNavController(navView,navController)

    mAdapter = NfcAdapter.getDefaultAdapter(this)

    fragmentStayAlive()

  

}

override fun onActivityResult(requestCode: Int,resultCode: Int,data: Intent?) {
    super.onActivityResult(requestCode,resultCode,data)
    for (fragment in supportFragmentManager.fragments) {
        fragment.onActivityResult(requestCode,data)
    }
}




fun ByteArray.toHexString() = joinToString("") { "%02x".format(it) }

private fun fragmentStayAlive(){

    val fragment1: Fragment = LobbyFragment()
    val fragment2: Fragment = GameSessionFragment()
    val fragment3: Fragment = UserStatusFragment()
    val fm: FragmentManager = supportFragmentManager
    var active = fragment1

    fm.beginTransaction().add(R.id.nav_host_fragment,fragment3,"UserStatusFragment").hide(
        fragment3
    ).commit();
    fm.beginTransaction().add(R.id.nav_host_fragment,fragment2,"GameSessionFragment").hide(
        fragment2
    ).commit();
    fm.beginTransaction().add(R.id.nav_host_fragment,fragment1,"LobbyFragment").commit();

    val mOnNavigationItemSelectedListener: BottomNavigationView.OnNavigationItemSelectedListener =
        object : BottomNavigationView.OnNavigationItemSelectedListener {
            override fun onNavigationItemSelected(item: MenuItem): Boolean {
                when (item.getItemId()) {
                    R.id.lobbyFragment -> {
                        fm.beginTransaction().hide(active).show(fragment1).commit()
                        active = fragment1

                        return true
                    }
                    R.id.gameSessionFragment -> {
                        fm.beginTransaction().hide(active).show(fragment2).commit()
                        active = fragment2


                        return true
                    }
                    R.id.userStatusFragment -> {
                        fm.beginTransaction().hide(active).show(fragment3).commit()
                        active = fragment3


                        return true
                    }
                }

                replaceFragment(active,null,true,true)
                return false


            }
        }
}

fun replaceFragment(
    fragment: Fragment,@Nullable bundle: Bundle?,popBackStack: Boolean,findInStack: Boolean
) {
    val fm = supportFragmentManager
    val ft: FragmentTransaction = fm.beginTransaction()
    val tag = fragment.javaClass.name

    val parentFragment: Fragment?
    parentFragment = if (findInStack && fm.findFragmentByTag(tag) != null) {
        fm.findFragmentByTag(tag)
    } else {
        fragment
    }
    // if user passes the @bundle in not null,then can be added to the fragment
    if (bundle != null) {
        parentFragment!!.arguments = bundle
    } else {
        parentFragment!!.arguments = null
    }
    // this is for the very first fragment not to be added into the back stack.
    if (popBackStack) {
        fm.popBackStack(null,FragmentManager.POP_BACK_STACK_INCLUSIVE)
    } else {
        ft.addToBackStack(parentFragment.javaClass.name + "")
    }
    ft.add(R.id.nav_view,parentFragment,tag)
    ft.commit()
    fm.executePendingTransactions()
}

'''

感谢所有帮手!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)