4

I am using a customized DialogFragment. In the onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) method, I inflate a layout that contains another fragment causing the app to crash. How can I fix it?

Abdalrahman Shatou
  • 4,285
  • 6
  • 45
  • 72

1 Answers1

10

You cannot nest Fragments in other Fragments with XML-Layouts. You have to add them with code. To insert a Fragment into another Fragment you have to use a special FragmentManager - a child FragmentManager. You can get it from the parent Fragment:

//In the DialogFragment ('parent') get the child FragmentManager:
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.transaction.add(new MyCustomChildFragment(), "CustomTag");

I usually add child Fragments in onActivityCreated().

thaussma
  • 9,422
  • 4
  • 41
  • 45