I have the following code (borrowed from the sunflower app) :
const val FRAGMENT_A_PAGE_INDEX = 0
const val FRAGMENT_B_PAGE_INDEX = 1
class ViewPagerAdapter(fragment: Fragment): FragmentStateAdapter(fragment) {
/**
* Mapping of the ViewPager page indexes to their respective Fragments
* */
private val tabFragmentsCreators: Map<Int, () -> Fragment> = mapOf(
FRAGMENT_A_PAGE_INDEX to { FragmentA() },
FRAGMENT_B_PAGE_INDEX to { FragmentB() }
)
override fun getItemCount(): Int = tabFragmentsCreators.size
override fun createFragment(position: Int): Fragment =
tabFragmentsCreators[position]?.invoke() ?: throw IndexOutOfBoundsException()
}
So, in my host fragment controller hosting the viewpager above, I have the following:
class ViewPagerControllerFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
binding = FragmentViewPagerControllerBinding.inflate(inflater, container, false)
// set the adapter of the ViewPager2 widget
binding.viewpager.adapter = ViewPagerAdapter(this)
binding.lifecycleOwner = viewLifecycleOwner
// inflate the layout for this fragment
return binding.root
}
... // NOT RELEVANT PARTS ARE SKIPPED ...
}
During a debug session, I saw that onCreateView()
methods of FragmentA
and FragmentB
gets only called/invoked once.
So, when FragmentA
is visited the first time, the system invokes onCreateView()
method of it. When I swipe to FragmentB
and then back to FragmentA
again, the onCreateView()
is not invoked.
Why is that so ? And how can I can change this so that it is invoked whenever I enter the Fragment?
class FragmentA : Fragment() {
// gets called once
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { ... }
}
class FragmentB : Fragment() {
// gets called once
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { ... }
}