I am managing the fragment as show and hide. But when I try to access the method in one fragment, I get null and I ask the question. Of course, method access works well in other fragments that manage the same.
This is my hide code.
private fun replaceFragment(fragment: Fragment) {
supportFragmentManager.beginTransaction().apply {
if (fragment.isAdded) {
show(fragment)
} else {
add(R.id.container_activity_main, fragment)
}
supportFragmentManager.fragments.forEach {
if (it != fragment && it.isAdded) {
hide(it)
}
}
}.commit()
}
my show code
supportFragmentManager.beginTransaction().show(fragmentName).commit()
fragmentName is arbitrarily specified for the question
If you take a log of the codes below, false, false, and true are displayed.
${fragmentName!!.isVisible}, ${fragmentName!!.isDetached} ,${fragmentName!!.userVisibleHint}
Here the fragment is visible to me but isVisible always return false . I don't know why this happens.
Even if onStop is called in hide , if you show, onCreateView is called and there should be a view, but the view becomes null and the app does not work.
I don't know why this is happening. The view is visible, but visible returns false.
For the visibility issue you should use :
fragment.userVisibleHint
instead of:
fragment.isVisible
Like mentioned in the documentation.
Fragment.isVisible doesn't work with all fragment use cases, it can be true only if fragment.isAdded == true, fragment.isHidden == false, fragment.view != null, fragment.view.windowToken != null and fragment.view.visibility == View.VISIBLE. Only if all these conditions are met in your implementation of the fragment, then the isVisible method returns true.