I have a viewpager containning two fragments, and a viewModel (stored in the parent activity) holding a record to the index of current page being visible.
here is the setUp of the parent fragment:
override fun setUp() {
super.setUp()
titles = arrayOf(R.string.providers, R.string.create_provider)
fragments = arrayOf(ProvidersListFragment(), CreateProviderFragment())
pagerAdapter = BaseFragmentAdapter(this, fragments = fragments)
}
override fun setUpViews() {
super.setUpViews()
binding.page.adapter = pagerAdapter
TabLayoutMediator(binding.tabs, binding.page) { tab, position ->
tab.text = getString(titles[position])
}.attach()
binding.page.currentItem = viewModel.currentPage.value!!
}
override fun setUpObservers() {
super.setUpObservers()
viewModel.currentPage.observe(viewLifecycleOwner, {
Log.d(TAG, "setUpObservers: it = $it")
if (it != binding.page.currentItem)
binding.page.currentItem = it
})
}
the 3 methods above are called inside onResume().
the ViewModel holds the record to the current index like this: var currentPage = MutableLiveData<Int>().apply { value = 1 }
technically the starting page should be index 1, my case the TabMediator shows as if the page 1 is selected, but the layout is showing the content of page 0 (not even fully loaded).
that's what it's showing:
and that's what it's supposed to show:
this is the layout 0:
any ideas about this behaviour and how can i avoid it?


