2

Hello I have created a simple app with a MainActivity extending ActionBarActivity. I have 4 tab Fragments and the action bar is linked with those 4. Now inside one of those tabs I want to include a YouTube video player.

I am struggling because I want to include a video fragment inside a fragment. I have created another app with an activity and a YoutubeSupportFragment which works ok but in my case I don't have an activity to include the video fragment but a fragment. Any ideas?

Blo
  • 11,903
  • 5
  • 45
  • 99
nick Georg
  • 77
  • 8

1 Answers1

2

You need to read about nested fragments. The basic difference is that you cannot add the nested fragment with xml tag <fragment>. You must add it by code, and for that you use getChildFragmentManager() instead of getFragmentManager(getSupportFragmentManager() if using support library) to commit the fragment transaction:

YouTubePlayerSupportFragment childFragment = YouTubePlayerSupportFragment.newInstance();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.replace(R.id.youtube_fragment_container, childFragment).commit();

Where R.id.youtube_fragment_container is a FrameLayout declared in the layout of the parent fragment.

Here's an example of nested fragments.

ILovemyPoncho
  • 2,762
  • 2
  • 24
  • 37
  • Great! Please don't forget to mark the answer as accepted if your problem is already solved. You can do it clicking on the check mark beside the answer to toggle it from hollow to green. – ILovemyPoncho Aug 12 '14 at 17:10