androidandroid-fragmentsandroid-viewpagerxamarinsupportmapfragment

Referencing the SupportFragmentManager in a fragment


I am coding a Google Maps application in Xamarin and have successfully extended the class to use Android.Support.V4.App.FragmentActivity.

I am now wanting to change the Android.Support.V4.App.FragmentActivity to be a Android.Support.V4.App.Fragment so that I can have a ViewPager that has a Google Map fragment.

As I have now changed the class to extend from Android.Support.V4.App.Fragment, I cannot reference the SupportFragmentManager.

Here is my code:

private void InitMapFragment()
{
    _mapFragment = SupportFragmentManager.FindFragmentByTag("map") as SupportMapFragment;
    if (_mapFragment == null)
    {
        GoogleMapOptions mapOptions = new GoogleMapOptions()
            .InvokeMapType(GoogleMap.MapTypeNormal)
            .InvokeZoomControlsEnabled(true)
            .InvokeCompassEnabled(true);

        FragmentTransaction fragTx = SupportFragmentManager.BeginTransaction();
        _mapFragment = SupportMapFragment.NewInstance(mapOptions);
        fragTx.Add(Resource.Id.map, _mapFragment, "map");
        fragTx.Commit();
    }
}

How do I reference the SupportFragmentManager in a Android.Support.V4.App.Fragment?

Thanks in advance


Solution

  • From a Fragment you can get the fragment Manager with getFragmentManager() or getChildFragmentManager() if you have nested fragments, But you still need your Activity to extend FragmentActivity, you can have the viewpager within it. Or you can have something like this:

    enter image description here

    so you can later replace the MainFragment with some other Fragment.