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.....

1 Answer 1

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...

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for replying. I am using (extended) SherlockFragment and it doesn't allow getChildFragmentManager(), Says undefined.Did do something wrong....
and also getParentFragment() says undefine too.Do you have any idea why do i get this koby
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 ..)
How to add two child nested fragment on that parent fragment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.