0

I have migrated from ViewPager to ViewPager2

Official guide & blog are helpful.

But I found that my code working in one of the pages (Fragment) of the ViewPager, located inside onResume is not being executed, when I come back to the Fragment hosting TabLayout & ViewPager.

If I change Tabs, onResume is correctly called.

I am using navigation controller of JetPack where ViewPager2 is second fragment & I am coming back from a regular third fragment. I am returning to the page from where I had called third fragment. So it is a simple back.

viewPager.setOffscreenPageLimit(tabLength) is in place, so that all tabs are loaded in one go. Also have viewPager.isSaveEnabled = false which loads the fragment in state as it was left, well preserved.

Have tried tab SelectedListener. But it is not called on back

tabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
   override fun onTabSelected(tab: TabLayout.Tab) {
     // not called on back
   }
}

My need is to trigger a function call on back for just one particular page, of ViewPager2

Rahul Kahale
  • 519
  • 6
  • 19
  • https://stackoverflow.com/questions/35436045/onresume-not-called-in-fragment-using-tablayout-and-viewpager this hasn't worked for me – Rahul Kahale Jan 23 '21 at 19:04
  • Which of the three constructors of viewpager2 are you using in the hosting Fragment? – Andrew Jan 23 '21 at 21:30
  • Using it this way https://stackoverflow.com/a/58711727/1029110 – Rahul Kahale Jan 24 '21 at 05:01
  • Adapter is class MyAdapter(fm: FragmentActivity) : FragmentStateAdapter(fm) { ...} There is no direct constructor for ViewPager2, but simple view binding using findViewById – Rahul Kahale Jan 24 '21 at 05:08
  • Sorry yes meant Adaptor constructor, have you tried using `MyAdapter(fm: Fragment) : FragmentStateAdapter(fm) { ...}` instead because I've seen that using using hosted in an `Activity` constructor in a `Fragment` to cause weird lifecycle state problems because you are using the same Fragment manager for the hosting fragment as the children of the viewpager, when you should be using a child fragment manager instead. see https://developer.android.com/reference/kotlin/androidx/viewpager2/adapter/FragmentStateAdapter#%3Cinit%3E(androidx.fragment.app.Fragment) – Andrew Jan 24 '21 at 09:32
  • I tried it. It does call onResume on back. But all the viewpager2 fragments are empty (even in layout inspector). Tabs are active with headers visible. But nothing in pager – Rahul Kahale Jan 24 '21 at 10:02
  • You might be doing something else wrong but you don't show enough of your code to determine this, but you should use "hosted in Fragment" constructor if hosting in a fragment. – Andrew Jan 24 '21 at 10:16

1 Answers1

0

Its fixed now.

I was expecting call inside onResume of one of the fragments of the Viewpager2 (pages).

The arrangement of MyAdapter(fm: FragmentActivity) :FragmentStateAdapter(fm) { ...} is reaching onResume of the second fragment, One which has the ViewPager2 adapter.

The requireActivity() inside onResume of the second fragment is returning instance of FragmentActivity which can be typeCasted to holding activity.

This arrangement is good enough for me to work with least issues where MyAdapter(fm: Fragment) is causing state preservation & restoration problems.

Thanks for help, Andrew.

Rahul Kahale
  • 519
  • 6
  • 19