質問

I am using the Android Compatibility Package with API Level 10. I have a FragmentActivity and in its onCreate method, I dynamically add a newly created Fragment instance (myPagerFragment) using:

getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, myPagerFragment).commit();

The myPagerFragment contains a ViewPager which makes use of a FragmentPagerAdapter. The implementation is according to the Android documentation with nothing special. When a button in one of the ViewPager child Fragments is clicked, then the myPagerFragment will be removed and replaced by a new Fragment instance (myOtherFragment) using:

FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);
ft.replace(R.id.fragment_container, myOtherFragment).commit();

The myOtherFragment displays a simple view containing a button. When this button is clicked, then the myOtherFragment will be removed and replaced again with a new instance of myPagerFragment using:

FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);
ft.replace(R.id.fragment_container, myPagerFragment).commit();

At this point the myPagerFragment no longer displays as it did in the beginning, but instead, displays a blank screen. There is no crash and nothing useful in the log.

I have tried this method with other views, for instance, replacing myPagerFragment with a Fragment that contains a simple view definition, and this works correctly and as expected. It is only Fragments that contain a ViewPager, that displays incorrectly at successive loads.

How can I make Fragments that contain a ViewPager to always display correctly?

役に立ちましたか?

解決

You cannot nest fragments in fragments. Hence, you cannot put a FragmentPagerAdapter in a ViewPager in a Fragment. Please redesign your UI to resolve this conflict, such as by getting rid of the outer-most Fragment.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top