How can I access getSupportFragmentManager() in a fragment?

Kotlin users try this answer

(activity as AppCompatActivity).supportFragmentManager

All you need to do is using

getFragmentManager()

method on your fragment. It will give you the support fragment manager, when you used it while adding this fragment.

Fragment Documentation


Simply get it like this -

getFragmentManager() // This will also give you the SupportFragmentManager or FragmentManager based on which Fragment class you have extended - android.support.v4.app.Fragment OR android.app.Fragment.

OR

getActivity().getSupportFragmentManager();

in your Fragment's onActivityCreated() method and any method that is being called after onActivityCreated().


You can directly call

getParentFragmentManager()  

to get the fragment manager. Note that getFragmentManager() also works but has been marked as deprecated.