1

I am having problems with fragments or maybe rather the way I want them to work.

In my activity I have a layout container (R.id.container1) to which I add a fragment (FragMain) programmatically at startup. In the fragment I have 2 Fragments (Frag1, Frag2) and a ViewPager which loads several other fragments (FragA, FragB, FragC, FragD, FragE) via a FragmentPagerAdapter.

On a button press I replace the whole content of R.id.container1, so FragMain is replaced with another Fragment (FragSub). Now when I press the back button, FragMain is loaded again, but the ViewPager isn't fully initialized, some Fragments are missing. From what I observed, it's always FragB and FragD missing, If I scroll to the empty views, the app crashes. The other fragments seem to be fine. What's going wrong here?

This thread suggests using getChildFragmentManager in creation of the PagerAdapter, but I am already doing that... Fragment in ViewPager not restored after popBackStack

I am accessing the fragments via e.g.

((FragA)((PagerAdapter)viewPager.getAdapter()).getItem(0));

Is this the best way?

Some Code :

MainActivity's onCreate

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    pagerFragList = new Vector<Fragment>();
    pagerFragList.add(Fragment.instantiate(this, FragA.class.getName()));
    pagerFragList.add(Fragment.instantiate(this, FragB.class.getName()));
    pagerFragList.add(Fragment.instantiate(this, FragC.class.getName()));
    pagerFragList.add(Fragment.instantiate(this, FragD.class.getName()));
    pagerFragList.add(Fragment.instantiate(this, FragE.class.getName()));

    FragMain fragMain = (FragMain)getSupportFragmentManager().findFragmentByTag(FragMain.debugTag);
    if(fragMain==null) fragMain = new FragMain();
    FragmentHelper.replaceSupportFragment(this, fragMain, R.id.container1, false, FragMain);

}

Helper

public Vector<Fragment> getFragList() {
    return pagerFragList;
}

FragMain's onViewCreated

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    pagerAdapter = new PagerAdapter(getChildFragmentManager(), ((MainActivity)getActivity()).getFragList());
    viewPager = ((ViewPager)view.findViewById(R.id.viewPager));
    viewPager.setAdapter(pagerAdapter);
    viewPager.setOffscreenPageLimit(5);
    viewPager.setCurrentItem(0, true);

}

The PagerAdapter

public class PagerAdapter extends FragmentPagerAdapter {

private List<Fragment> fragments;

public PagerAdapter(FragmentManager fm, List<Fragment> fragments) {
    super(fm);
    this.fragments=fragments;
}

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

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

}

Community
  • 1
  • 1
El Duderino
  • 855
  • 1
  • 10
  • 21

2 Answers2

1

Have no idea why this seems to be fixing it on my end, but try commenting out your "viewPager.setOffscreenPageLimit(5);" line.

I'm still investigating and will be back with more if I find anything else useful out.


The other thing it turns out that I had to do was override the "state" methods of a FragmentStatePagerAdapter like this:

@Override
public Parcelable saveState() {
    return null;
}

@Override
public void restoreState(Parcelable state, ClassLoader loader) {
    //do nothing
}

In order to bypass whatever strange thing that was causing problems (must have had something to do with saving and restoring the states of nested fragments in a ViewPager...

Shep Shapard
  • 148
  • 8
0

There are alot of permutations involving the state of fragments and backstack transitions. For debug, it may help to isolate the event you are working with (backbutton , orientation change ... ) and to look at the following before and after your event....

state of fragment manager which includes the backstack i believe...

//debug
        getFragmentManager().dump("", null, 
                 new PrintWriter(System.out, true), null);

see this thread

Community
  • 1
  • 1
Robert Rowntree
  • 6,230
  • 2
  • 24
  • 43