My activity uses TabLayout
+ ViewPager
.
The number of tabs and pages here are dynamic depending on the data fetch from the server.
Th
The FragmentPagerAdapter
already caches the Fragments
for you. Each fragment is assigned a tag, and then the FragmentPagerAdapter
tries to call findFragmentByTag
. It only calls getItem() if the result from findFragmentByTag
is null
.
You're probably getting this error because you're adding the same fragment instance to the list. You should create a new instance for each page.
Example form document :
//...
public static class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
@Override
public int getCount() {
return NUM_ITEMS;
}
@Override
public Fragment getItem(int position) {
return ArrayListFragment.newInstance(position);// IMPORTANT
}
}
//..
Refer :FragmentPagerAdapter
Main thread : Retrieve a Fragment from a ViewPager