[android] How can I access getSupportFragmentManager() in a fragment?

I have a FragmentActivity and I want to use a map fragment within it. I'm having a problem getting the support fragment manager to access it.

 if (googleMap == null) {
            googleMap = ((SupportMapFragment) getSupportFragmentManager()

            // check if map is created successfully or not
            if (googleMap == null) {
                        "Sorry! unable to create maps", Toast.LENGTH_SHORT)

            // create marker
            MarkerOptions marker = new MarkerOptions().position(
                    new LatLng(latitude, longitude)).title("Hello Maps ");

            CameraPosition cameraPosition = new CameraPosition.Builder()
                    .target(new LatLng(latitude, longitude)).zoom(15).build();


            // adding marker

The answer is

You can directly call


to get the fragment manager.


In your fragment,

Create field :

private FragmentActivity myContext;

override onAttach method of your fragment :

public void onAttach(Activity activity) {
    myContext=(FragmentActivity) activity;

When you need to get Support fragment manager call :

FragmentManager fragManager = myContext.getSupportFragmentManager(); //If using fragments from support v4


FragmentManager fragManager = myContext.getFragmentManager();

You are done.

Similar questions with android tag:

Similar questions with android-fragments tag:

Similar questions with android-support-library tag:

Similar questions with android-fragmentactivity tag:

Similar questions with fragmentmanager tag: