Fragment re-created on bottom navigation view item selected

后端 未结 15 2550
长发绾君心
长发绾君心 2020-12-07 23:55

Following is my code for bottom navigation view item selected

bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationI         


        
15条回答
  •  感情败类
    2020-12-08 00:47

    I wrote an Extension function in Kotlin for the same.

    fun FragmentManager.switch(containerId: Int, newFrag: Fragment, tag: String) {
    
    var current = findFragmentByTag(tag)
    beginTransaction()
        .apply {
    
            //Hide the current fragment
            primaryNavigationFragment?.let { hide(it) }
    
            //Check if current fragment exists in fragmentManager
            if (current == null) {
                current = newFrag
                add(containerId, current!!, tag)
            } else {
                show(current!!)
            }
        }
        .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
        .setPrimaryNavigationFragment(current)
        .setReorderingAllowed(true)
        .commitNowAllowingStateLoss()
    }
    

提交回复
热议问题