Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - DialogFragment with Layout Containing Fragment

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?

like image 280
Abdalrahman Shatou Avatar asked Jan 13 '23 21:01

Abdalrahman Shatou


1 Answers

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

like image 114
thaussma Avatar answered Jan 16 '23 00:01

thaussma