Fragments in the viewpager only disappear after 2 turns

I'm having a weird issue with fragments inside a viewpager. If I rotate the device once everything seems to work and my fragments are loaded with all their data. However, on the second rotation, the fragments inside my viewer seem to disappear. If I check isAdded () it returns true, however isVisible () returns false. Is there a reason why the snippet will be added to my viewer but not displayed?

Here's my snippet that contains the viewer:

public class DrawerFragment extends ParentFragment{

ViewPager viewPager;

GradingFragmentPagerAdapter pagerAdapter;
ArrayList<ParentFragment> mGradingFragments;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setRetainInstance(true);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);

    viewPager = (android.support.v4.view.ViewPager) rootView.findViewById(R.id.pager);
    if(mFragments == null){
     mFragments = new ArrayList<ParentFragment>();
     mFragments.add(new FragmentA());
     mFragments.add(new FragmentB());
    }
    pagerAdapter = new MyFragmentPager(getChildFragmentManager(), mFragments);
    viewPager.setAdapter(pagerAdapter);

    return rootView;
}

}

      

Here's my PagerAdapter class

 public class MyFragmentPagerAdapter extends FragmentPagerAdapter {

    ArrayList<ParentFragment > fragments;

    public GradingFragmentPagerAdapter(android.support.v4.app.FragmentManager fm, ArrayList<ParentFragment> fragments){
        super(fm);
        this.fragments = fragments;
    }

    @Override
    public ParentFragment getItem(int position) {
        return fragments.get(position);
    }

    @Override
    public int getCount() {
        return fragments.size();
    }

}

      

What I don't understand is that it only seems to break in the second spin. If I set the background color on the viewpager, after two rotations, the area where my fragments should be is the background color in the view. However, if I look at it in debug mode, the fragment is added to the pagerAdapter. They are simply not visible.

Also it doesn't seem like onCreateView is getting called on 2 fragments inside the viewpager. I'm not entirely sure if this is the expected behavior or not when using setRetainInstance (true). From what I understand, setting it to the parent fragment should handle its child fragments as well. But isn't it necessary to recreate all kinds of child fragments every turn?

+3
android android-fragments android-viewpager


source to share


No one has answered this question yet

Check out similar questions:

1296
Reloading activity on android rotation
937
findViewById in fragment
823
onActivityResult is not called on fragment
702
How to detect when a fragment becomes visible in the ViewPager
572
ViewPager PagerAdapter does not update View
462
ViewPager and Fragments - What's the correct way to store the state of a Fragment?
447
IllegalStateException: Cannot complete this action after onSaveInstanceState with ViewPager
29
Replacing the ViewPager with a Fragment - then navigating back
3
Where to restore the state of the fragment inside the ViewPager
0
Can't get support for FragmentManager inside Fragment - ViewPager inside Fragment



All Articles
Loading...
X
Show
Funny
Dev
Pics