0

I'm an Android application that have one activity that have a ViewPager with 4 Fragments.

Now I'm building a OnPageChangeListener and in the method onPageSelected I want to get the current fragment that the user can see. So I have used this code:

public void onPageSelected(int position) {
                Fragment currFragment = null;
                FragmentManager fm = getSupportFragmentManager();
                List<Fragment> fragments = fm.getFragments();
 //TO DO
}

but the list of Fragment contains only 3 Fragment instead of 4. Why ?

How can I get the current Fragment ?

bircastri
  • 2,169
  • 13
  • 50
  • 119
  • It is probably because at a same there is only three instances of the fragment, generated by the ViewPagerAdapter. You need to implement more, maybe use some interfaces between your activity and fragments. – Reza Bigdeli May 16 '18 at 07:19
  • also try to get the second fragment from your list, it is probably the current fragment: `fragments.get(1)` – Reza Bigdeli May 16 '18 at 07:21

0 Answers0