user3548779
user3548779

Reputation: 321

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

Upvotes: 5

Views: 24115

Answers (2)

Dev Alase
Dev Alase

Reputation: 11

may be your MainActivity class is inheriting from ComponentActivity, then change it to AppCompatActivity as supportFragmentManager is supported by AppCompatActivity class. this won't make considerabale damage to your previous code

If in case app crashes use FragmentActivity instead of AppComatActivity to inherit from

Upvotes: 0

ILovemyPoncho
ILovemyPoncho

Reputation: 2792

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.

Upvotes: 5

Related Questions