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()
.findFragmentById(R.id.map1)).getMap();
// check if map is created successfully or not
if (googleMap == null) {
Toast.makeText(getApplicationContext(),
"Sorry! unable to create maps", Toast.LENGTH_SHORT)
.show();
}
}
// 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();
googleMap.animateCamera(CameraUpdateFactory
.newCameraPosition(cameraPosition));
// adding marker
googleMap.addMarker(marker);
All you need to do is using
method on your fragment. It will give you the support fragment manager, when you used it while adding this fragment.
Fragment Documentation
You can directly call
to get the fragment manager.
or
In your fragment,
Create field :
override onAttach method of your fragment :
When you need to get Support fragment manager call :
or
You are done.
getActivity().getFragmentManager()
This worked or me.Also take a look at here.
Try this:
You can use
getActivity().getSupportFragmentManager()
anytime you want to getSupportFragmentManager.hierarchy is Activity -> fragment. fragment is not capable of directly calling getSupportFragmentManger but Activity can . Thus, you can use getActivity to call the current activity which the fragment is in and get
getSupportFragmentManager()
My Parent Activity extends AppCompatActivity so I had to cast my context to AppCompatActivity instead of just Activity.
eg.