37

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?

Blo
  • 11,903
  • 5
  • 45
  • 99
Gonzalo
  • 3,674
  • 2
  • 26
  • 28

5 Answers5

54

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

Akhil Dad
  • 1,804
  • 22
  • 35
Eurig Jones
  • 8,226
  • 7
  • 52
  • 74
38

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

Community
  • 1
  • 1
muyiou
  • 679
  • 5
  • 15
  • 2
    Also 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
7

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)

Aibol Kussain
  • 391
  • 4
  • 7
5

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(), "");
Ameen Maheen
  • 2,719
  • 1
  • 28
  • 28
0

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());
pintu236
  • 148
  • 2
  • 11