Referencing the SupportFragmentManager in a fragment

18,252

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.

Share:
18,252
user3548779
Author by

user3548779

Updated on June 05, 2022

Comments

  • user3548779
    user3548779 almost 2 years

    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

  • user3548779
    user3548779 almost 10 years
    How do I call the SupportFragmentManager, not the FragmentManager?
  • ILovemyPoncho
    ILovemyPoncho almost 10 years
    from a Fragment you can all getActivity().getSupportFragmentManager()