Posted by blogmeister on
December 7, 2013
This one seems to hassle developers who have just started using fragments. Take this scenario for example:
Main Container > View Pager > Fragment objects
The problem is when I put in 2 ViewPager objects dynamically in the main container via the FragmentManager and FragmentTransaction classes respectively. The option addToBackStack() is also included. Now, after the back key is pressed, the previous ViewPager should be displayed.
However, nothing is shown and that left me scratching my head for quite a few hours. The solution is to pass a ChildFragmentManager instance to the FragmentStatePagerAdapter constructor. Do not use FragmentManager as that will make it blank.
Also, make sure your adapter uses FragmentStatePagerAdapter instead of FragmentPagerAdapter.
With those 2 requirements on hand, your ViewPager should be displayed no matter where they are in the FragmentManager’s pop stack.Found this useful? Donations appreciated to help keep this blog alive.