I'm migrating my ViewPager
to ViewPager2
since the latter is supposed to solve all the problems of the former. Unfortunately, when using it with a FragmentStateAdapter
, I don't find any way to get the currently displayed fragment.
viewPager.getCurrentItem()
gives the current displayed index and adapter.getItem(index)
generally creates a new Fragment
for the current index. Unless keeping a reference to all created fragments in getItem()
, I have no idea how to access the currently displayed fragment.
With the old ViewPager
, one solution was to call adapter.instantiateItem(index)
which would return the fragment at the desired index.
Am I missing something with ViewPager2
?
was facing same issue now its solved by adding one object in adapter
after creating adapter I registered my Viewpager 2 with
ViewPager2.OnPageChangeCallback()
and overrided its methodonPageSelected
now simple did this trick to get current fragment
cheers guys , hope this mayhelp you.!