I have an Activity
with a single Fragment
inside of it. Inside this Fragment I have a ViewPager
that pages Fragments. This makes the paged Fragments children of the Fragment inside the Activity. Basically like this:
Activity
|
|
Fragment
|
(ViewPager)
| |
| |
Fragment Fragment
Every so often, seemingly at random, the Fragments inside the ViewPager will not display. I looked at the View hierarchy in Hierarchy Viewer and noticed that when this happens the child Fragments (at the bottom of my picture) are in the layout, but the root View of the children has zero height and zero width (as does every View in the hierarchy tree from there down to the leaf Views). The ViewPager
does have height and width, but the Fragments it pages do not.
If I rotate the device at this point, my child Fragments show up!
I'm at a loss as to why this is happening. The Adapter I'm using with the ViewPager requires a FragmentManager
, and I'm using the one from the Fragment inside the Activity by calling getChildFragmentManager(). I feel like I'm doing everything I'm supposed to do, not sure where to look next on this one.
UPDATE:
Interestingly enough, if I put the code that attaches the Adapter to the ViewPager in a Runnable and post it to a Handler the issue is 100% reproducible. i.e.
class MyFragment extends Fragment {
@Override
public void onActivityCreated(Bundle b) {
super.onActivityCreated(b);
runPager = new Runnable() {
@Override
public void run() {
viewPager.setAdapter(adapter);
}
};
handler.post(runPager);
}
}