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?
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()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With