Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add fragment into a fragment (nested fragment)

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

like image 639
Mohamed Sabri Rauf Avatar asked Aug 14 '13 04:08

Mohamed Sabri Rauf


People also ask

Can we add fragment inside another fragment?

Fragments are also capable of hosting one or more child fragments. Inside a fragment, you can get a reference to the FragmentManager that manages the fragment's children through getChildFragmentManager() . If you need to access its host FragmentManager , you can use getParentFragmentManager() .

Which of the following is used to add fragment inside fragment?

Since FragmentContainerView is a subclass of FrameLayout, a Fragment can be added to it in the same way it can be added to a FrameLayout. Another way of adding a Fragment to a FragmentContainerView is by using the android:name attribute in the XML layout file.

What is nested fragment?

Fragments represent a behavior or portion of UI in an Activity. Fragments are mostly used in tablets to divide screen and utilize screen space in efficient way. With Android 4.2 nested fragments are introduced, with which now you can embed fragments inside fragments.


1 Answers

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

like image 69
koby Avatar answered Sep 27 '22 21:09

koby