6

I want to add a youtube fragment into my already existing fragments dynamically. The code I used is below:

        // setting the Youtube Player Dynamically
private int setYoutubePlayer(String desc, View view, int prevID,
        Bundle input) {

    if (desc.indexOf("=") != -1) {
        desc = desc.substring(desc.indexOf("=") + "=".length());
    } else {
        return prevID;
    }

    final String url = desc;

    LinearLayout videoLayout = new LinearLayout(view.getContext());
    videoLayout.setOrientation(LinearLayout.VERTICAL);
    prevID++;
    videoLayout.setId(prevID);

    FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager
            .beginTransaction();

    fragment.setVideoId(url);
    LinearLayout itemLayout = (LinearLayout) view.findViewById(R.id.items);
    itemLayout.addView(videoLayout);

    fragmentTransaction.add(itemLayout.getId(), fragment,
            "youtube fargment " + prevID);

    fragmentTransaction.addToBackStack(null);
    fragmentTransaction.commit();

    return prevID;
}

I need to get the youtube fragment in the appropriate fragment. As I checked when always a new fragment get loaded (when swipe between fragments), the new inner fragment needs to be the first loaded fragment.

Any help will be gladly accepted.

SOLVED: Thank you Koby You were right. i had to replace "getActivity().getSupportFragmentManager();" with "getChildFragmentManager()". The problem was apparently the Sherlock library came with a old android v4 support library. I had to update the support library in the Sherlock. It worked for me.....

Blo
  • 11,903
  • 5
  • 45
  • 99

1 Answers1

5

to create a nested fragment inside a fragment, you should use:

http://developer.android.com/reference/android/app/Fragment.html#getChildFragmentManager()

call the getChildFragmentManager() from the parent fragment, and do the transaction in the parent to nest the child inside.

https://stackoverflow.com/a/13381825/1693674

tell me if you need more help in doing that...

Community
  • 1
  • 1
koby
  • 136
  • 3
  • Thank you for replying. I am using (extended) SherlockFragment and it doesn't allow getChildFragmentManager(), Says undefined.Did do something wrong.... – Mohamed Sabri Rauf Aug 14 '13 at 08:03
  • and also getParentFragment() says undefine too.Do you have any idea why do i get this koby – Mohamed Sabri Rauf Aug 14 '13 at 08:14
  • i'm actually not a big fan of SherlockFragment, but lets give it a try.. is the SherlockFragment includes the support.v4 ? if not, you can change it to do so, then you will be able to get the childfragment.. (im shooting in the dark, so if it doasnt work - upload some code and maybe we can find other solutions ..) – koby Aug 20 '13 at 13:07
  • How to add two child nested fragment on that parent fragment – Vishvendu Palawat Jun 22 '17 at 12:00