viewpager默认只缓存3个fragment,当前和前后2个fragment,如果viewpager中有4个fragment,滑动到第三个fragment的时候,第一个fragment就会被销毁
解决办法是重写FragmentPagerAdapter适配器的instantiateItem、destroyItem方法
@Override
@NonNull
public Object instantiateItem(@NonNull ViewGroup container, int position) {
Fragment fragment = (Fragment)super.instantiateItem(container, position);
fm.beginTransaction().show(fragment).commitAllowingStateLoss();
return fragment;
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
Fragment fragment = (Fragment)object;
fm.beginTransaction().hide(fragment).commitAllowingStateLoss();
}