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?
Asked
Active
Viewed 5,819 times
4
![](../../users/profiles/1149647.webp)
Abdalrahman Shatou
- 4,285
- 6
- 45
- 72
-
Yes, more info please! Why is the cause of the crash according to Logcat? Can you show the code you are talking about including this other Fragment – mgibson Jul 18 '13 at 15:13
-
My question is simple :) How do you place a fragment inside a dialog fragment? – Abdalrahman Shatou Jul 18 '13 at 17:58
1 Answers
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()
.
![](../../users/profiles/821894.webp)
thaussma
- 9,422
- 4
- 41
- 45
-
i couldn't understand your answer. getChildFragmentManager() is not available for DialogFragment. – Abdalrahman Shatou Jul 18 '13 at 17:58
-
1Sure it is: https://developer.android.com/reference/android/app/Fragment.html#getChildFragmentManager() – thaussma Jul 18 '13 at 19:13
-
3just tried to nest Fragments in a DialogFragment with support library and it just works :) – Gianluca P. Nov 13 '13 at 12:12