I am using ViewPager2 with Tablayout . Initially everything works fine. ie, both swipe and clicking on Tab ,loads the correct fragment. Say I have 3 fragments Tab A, Tab B and Tab C. I need to open other fragments from Tab B ,say Tab B1.Tab B1 is opened and on pressing back button it goes back to Tab B. Now when I click on Tab A, it higlights TabA, but the fragment for Tab A is not loaded. But if we swipe, its working properly . Below is my Viewpager code.
ViewPager code
private val fragmentList = arrayListOf<BaseMvvmFragment<*, *>>(
TabAFragment(),
TabBFragment(),
TabCFragment(),
TabDFragment(),
)
private fun setupPager() {
val adapter = ViewPagerAdapter(
fragmentList,
childFragmentManager,
lifecycle
)
binding.mainViewPager.adapter = adapter
TabLayoutMediator(binding.pageIndicator, binding.mainViewPager) { tab, position ->
fragmentList[position].titleRes?.let {
tab.text = getString(it)
}
}.attach()
binding.mainViewPager.setCurrentItem(
fragmentList.indexOfFirst { it is TabAFragment },
false
)
}
Not able to figure out what's happening. Could someone help ?