getParentFragment returning null

回眸只為那壹抹淺笑 提交于 2019-11-27 14:35:13
Eurig Jones

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

muyiou

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

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

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)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!