I have a Fragment
that has a FrameLayout
. This first fragment (A) loads inside its Framelayout another fragment (B). When I call getParentFragment
from inner fragment (B), I get null
. How should this method be used properly?
5 Answers
getParentFragment() was introduced in API level 17 (Android 4.2). Android 4.2 introduced the idea of nested fragments (fragments containing other fragments). Calling this results in null if the fragment has a parent which is an Activity.
Have a look at this.
If you are using support library then you can use getParent(), may be you need to use getChildFragmentManager() while doing fragment transaction. See this

- 1,804
- 22
- 35

- 8,226
- 7
- 52
- 74
-
4Thank you! I used setTargetFragment instead. I know it's not meant for this purpose, but does the job. – Gonzalo Feb 11 '13 at 06:53
-
2It was introduced in 4.2 but can be used in earlier version using support library – Shashank Kumar May 13 '15 at 10:33
-
why does findFragmentByTag doesn't work inside child fragment (i.e nested)' – eC Droid Oct 16 '17 at 10:16
In my case, although my fragmentA was nested in fragmentB,but I still get null after call getParentFragment in FragmentA. Finally I found that I should use getChildFragmentManager rather than getFragmentManager in FragmentB.
check this What is difference between getSupportFragmentManager() and getChildFragmentManager()?
-
2Also note that [nested Fragments](http://developer.android.com/about/versions/android-4.2.html#NestedFragments) have the limitation that you cannot inflate a layout into a `Fragment` when that layout includes a
tag. Nested `Fragments` are only supported when added to a `Fragment` dynamically via `getChildFragmentManager()`. – jenzz Nov 08 '13 at 20:00
The one thing that helped is, when creating adapter use getChildFragmentManager()
.
If you are not using adapter, just use getChildFragmentManager()
when doing transactions.
setTargetFragment()
is not recommended, since it gives errors on moveState()
of fragment(because fragments should be tied to FragmentManager)

- 391
- 4
- 7
I faced the same issue , and fixed the issues by hosting second fragment in your parent fragment with getChildFragmentManager()
then you wont be getting the null value ...
Parent fragment
SignUpFragment signUpFragment = new SignUpFragment();
getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.contentPanel, signUpFragment)
.addToBackStack(null).commit();
Child fragment : what i have used is a dialog
HospitalCardDialog hospitalCardDialog = new HospitalCardDialog();
hospitalCardDialog.show(getChildFragmentManager(), "");

- 2,719
- 1
- 28
- 28
Just in case someone using viewpager inside fragment and facing same issue. Like if you want parent viewmodel using getParentFragment() inside child fragment. You need to use constructor with fragment not with fragmentActivity
Do not use this
public PagerAdapter(@NonNull FragmentActivity fragmentActivity, Bundle arguments) {
super(fragmentActivity);
this.arguments = arguments;
}
But use this
private static class PagerAdapter extends FragmentStateAdapter {
private final Bundle arguments;
public PagerAdapter(@NonNull Fragment fragment, Bundle arguments) {
super(fragment);
this.arguments = arguments;
}
@NonNull
@Override
public Fragment createFragment(int position) {
switch (position) {
case 1:
return MeterPhotoUploadFragment.newInstance(arguments);
default:
return ServiceConnectionDetailsFragment.newInstance(arguments);
}
}
@Override
public int getItemCount() {
return 2;
}
}
Usage:
PagerAdapter pagerAdapter = new PagerAdapter(this, getArguments());

- 148
- 2
- 11