Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Android Fragments Managing Fragments Adding the ViewPager

Iskandar Fendi
Iskandar Fendi
5,700 Points

getChildFragment(): fragment within fragment?

Can somebody explain to me what does it mean by "Fragment within fragment"? In this scenario? and the interesting things is when I changed, inside OnCreateView method, from getChildFragmentManager to getFragmentManager? It will run ok

1 Answer

Boban Talevski
Boban Talevski
24,793 Points

It means that we are adding a fragment within another fragment. In this case, the fragment (or fragments) we are adding are DirectionsFragment and IngredientsFragment and we are adding them to ViewPagerFragment.

Was curious to try using getFragmentManager instead of getChildFragmentManager as you suggested, but it only partially works, i.e. it works only on opening a recipe once, the ViewPager is initialized and fragments (within a fragment) are added and so on, but if you tap the back button to go back to the list, and choose another, or even the same recipe, we get a blank screen, which I believe means we are seeing ViewPagerFragment, but the actual ViewPager isn't initializing with the additional fragments (within the fragment), so that's probably the issue why we have to use getChildFragmentManager to be able to add a fragment within a fragment.