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?

5 Answers 5

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

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you! I used setTargetFragment instead. I know it's not meant for this purpose, but does the job.
It was introduced in 4.2 but can be used in earlier version using support library
why does findFragmentByTag doesn't work inside child fragment (i.e nested)'
37

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

1 Comment

Also note that nested Fragments have the limitation that you cannot inflate a layout into a Fragment when that layout includes a <fragment> tag. Nested Fragments are only supported when added to a Fragment dynamically via getChildFragmentManager().
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)

Comments

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(), "");

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.