I use fragmentManager.beginTransaction() but hide function is not working?
I want the titlefragment's screen to not disappear when the bottom navigation is switched. But I write. The recyclerview data of the titlefragment will overlap twofragment and threefragment. But why is it still displayed when I have hide??
this mainactivity
class MainActivity : AppCompatActivity() {
private val titleFragment = TitleFragment()
private val twoFragment = TwoFragment()
private val threeFragment = ThreeFragment()
private val fragmentManager = supportFragmentManager
private var activeFragment: Fragment = titleFragment
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
fragmentManager.beginTransaction().apply {
add(R.id.tragmentContainerView, threeFragment, "user").hide(threeFragment)
add(R.id.tragmentContainerView, twoFragment, "after").hide(twoFragment)
add(R.id.tragmentContainerView, titleFragment, "mind")
}.commit()
initListeners()
val bottomNavigationView = findViewById<BottomNavigationView>(R.id.bottomNavigationView)
val navHostFragment = supportFragmentManager.findFragmentById(R.id.tragmentContainerView) as NavHostFragment?
if (navHostFragment != null) {
val controller = navHostFragment.navController
NavigationUI.setupActionBarWithNavController(this, controller)
controller.addOnDestinationChangedListener { _, destination, _ ->
if(destination.id == R.id.titleFragment ||destination.id == R.id.twoFragment||destination.id == R.id.threeFragment) {
bottomNavigationView.visibility = View.VISIBLE
} else {
bottomNavigationView.visibility = View.GONE
}
}
}
}
private fun initListeners() {
bottomNavigationView.setOnItemSelectedListener { menuItem ->
when (menuItem.itemId) {
R.id.titleFragment -> {
fragmentManager.beginTransaction().hide(activeFragment).show(titleFragment).commit()
activeFragment = titleFragment
true
}
R.id.twoFragment -> {
fragmentManager.beginTransaction().hide(activeFragment).show(twoFragment).commit()
activeFragment = twoFragment
true
}
R.id.threeFragment -> {
fragmentManager.beginTransaction().hide(activeFragment).show(threeFragment).commit()
activeFragment = threeFragment
true
}
else -> false
}
}
}
override fun onSupportNavigateUp(): Boolean {
return super.onSupportNavigateUp()||findNavController(R.id.tragmentContainerView).navigateUp()
}
}
Please let me know if there is any shortage
That's not how fragments work, when you add fragment to fragment manager, objects of fragments get stored in fragmentmanager's backstack. With multiple fragments they do not get rendered at the same time so that it would work with just show and hide. Therefore show and hide do not work. Only a single fragment gets rendered in fragment container which is on the top of the fragment backstack.
Therefore you can add you first fragment to the backstack after setContentView
setContentView(R.layout.activity_main)
fragmentManager().beginTransaction().add(R.id.tragmentContainerView, titleFragment,"user")
.commit();
and then just simply replace the top fragment from backstack,
fragmentManager().beginTransaction().replace(R.id.tragmentContainerView, twoFragment,"after")
.commit();
However you can keep adding fragments to backstack using first statement, and then you can remove the fragment from top of the backstack using this command.
fragmentManager().popBackStack();
Therefore you function will be like this
private fun initListeners() {
bottomNavigationView.setOnItemSelectedListener { menuItem ->
when (menuItem.itemId) {
R.id.titleFragment -> {
fragmentManager.beginTransaction().replace("R.id.tragmentContainerView",titleFragment,"user").commit()
true
}
R.id.twoFragment -> {
fragmentManager.beginTransaction().replace("R.id.tragmentContainerView",twoFragment,"after").commit()
true
}
R.id.threeFragment -> {
fragmentManager.beginTransaction().replace("R.id.tragmentContainerView",threeFragment,"mind").commit()
true
}
else -> false
}
}