androidandroid-fragmentsandroid-nested-fragment

getParentFragment returning null


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?


Solution

  • 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