1

I am trying to display a nested ListFragment inside my DialogFragment.

Apparently I cannot just declare a <fragment> in the XML for my DialogFragment layout because fragments-in-fragments need the childFragmentManager. So I am trying to do this in my DialogFragment:

Fragment listfragment = new ClassThatExtendsListFragment();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(????????, listfragment).commit();

I have absolutely no idea what resource ID I need to put in the ???????? section, or how I'd even go about assigning it.

AJJ
  • 2,004
  • 4
  • 28
  • 42

3 Answers3

2

simply add FrameLayout in you layout. suppose you gave it's id as "container",

Fragment exampleFragment = new ExampleFragment();
    getSupportFragmentManager().beginTransaction()
            .add(R.id.container, exampleFragment).commit();
ErShani
  • 392
  • 2
  • 9
1

My requirement also was same like you. below code worked for me.

FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    MyTripFragment myTripFragment = new MyTripFragment();
    fragmentTransaction.add(R.id.fragment_container, myTripFragment);
    fragmentTransaction.commit();

XML code:

 <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</LinearLayout>

Hope it will help for you.

Venu
  • 348
  • 4
  • 17
0

I'm not sure what type of layout you're using for your DialogFragment, but generally in the XML that DialogFragment inflates you need to add a FrameLayout and importantly give it an ID. Then when you do your fragment transaction you pass in the resource id of that FrameLayout

XML:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></FrameLayout>
</LinearLayout>

If you want to use nested fragments you'll need to call getChildFragmentManager():

FragmentManager fragmentManager = getChildFragmentManager()

Then for your fragment transaction:

FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

fragmentTransaction.replace(R.id.fragment_container, new MyFragment()).commit();

You might want to use the add method instead of replace, but thats up to you

You might also want to add the previous fragment to the backstack if you want the enable back button presses:

fragmentTransaction.replace(R.id.fragment_container, new MyFragment()).addToBackStack(null).commit();
Simon
  • 9,762
  • 15
  • 62
  • 119
  • This is a fragment in a fragment so you can't use the regular manager. Also this code I assume goes in the oncreatedialog method of my dialogfragment? – AJJ Apr 05 '16 at 05:04
  • Didn't notice that. In that case you would just use childfragmentmanager instead of the regular fragmentmanager. What you pass into the add/replace method as first argument is going to be the same (the resource id of the framelayout where you want the sub fragment to be displayed) – Simon Apr 05 '16 at 07:55
  • Where you place this code depends on where/when you want your ListFragment to be displayed. As soon as you execute the code above the fragment transaction will be committed and things will be displayed. So just place it in the appropriate place for when you want the new fragment to come to life – Simon Apr 05 '16 at 07:58
  • How do I know to use add or replace? What if I'm using up support fragments? – AJJ Apr 05 '16 at 12:21
  • What is the minimum SDK/API version you're targetting? – Simon Apr 05 '16 at 18:18
  • I think this might be related to the DialogFragment rather than nesting fragments: http://stackoverflow.com/questions/20303865/viewpager-in-dialogfragment-illegalstateexception-fragment-does-not-have-a-vi – Simon Apr 05 '16 at 18:23