xamarin.formsxamarin.androidmapboxmapbox-androidcustom-renderer

Custom MapBox Android Renderer - crash when change tabs


I am trying to create a MapBox Renderer for Android in Xamarin Forms using the naxam library. The map is displaying well as a content of a view in a tab, however when I change tab several times it crashes on the android emulator. I suppose the problem comes from the renderer but can't figure out where.

Here is a part of my code :

public class MapViewRenderer : ViewRenderer<ContentView, Android.Views.View>,
    IOnMapReadyCallback, Com.Mapbox.Mapboxsdk.Maps.Style.IOnStyleLoaded
{
    private MapView _mapView = null;
    private MapboxMap _mapBox = null;
    public MapViewRenderer(Context context) : base(context)
    {
        Mapbox.GetInstance(Context, "pk.###");
    }


    public void OnMapReady(MapboxMap p0)
    {
        _mapBox = p0;
        [...]
    }
    public void OnStyleLoaded(Com.Mapbox.Mapboxsdk.Maps.Style p0)
    {
        [...]
    }
    protected override void OnElementChanged(ElementChangedEventArgs<ContentView> e)
    {
        base.OnElementChanged(e);
        if (e.NewElement != null)
        {
            _mapView = new MapView(Context);
            _mapView.GetMapAsync(this);
            view.Content = _mapView.ToView();
        }
    }
    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            _mapView.Dispose();
        }
        base.Dispose(disposing);
    }
}

All the examples I read on the net are about creating the MapView in the main activity with OnCreate, OnStart, OnResume... I haven't found any about creating a map in a custom render.

Please help.

EDIT :

---------------------------------SOLUTION---------------------------------

The problem was fixed using the code below in the custom renderer. In addition the renderer uses a Mapview instance which has been moved inside the main activity following ToolmakerSteve's remark.

MapViewRenderer.cs :

public class MapViewRenderer : ViewRenderer<ContentView, Android.Views.View>
{
    public MapViewRenderer(Context context) : base(context)
    {}

    protected override void OnElementChanged(ElementChangedEventArgs<ContentView> e)
    {
        base.OnElementChanged(e);
        if (e.NewElement != null)
        {
            var view = e.NewElement as Views.Map;
            if (Control == null)
            {
                SetNativeControl(MainActivity.MainActivityInstance.MapView);
            }
        }
    }
    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            MainActivity.MainActivityInstance.MapView.RemoveFromParent();
        }
        base.Dispose(disposing);
    }
}

MainActivity.cs :

public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity, IOnMapReadyCallback, Style.IOnStyleLoaded
{
    public MapView MapView { get; private set; } = null;
    public MapboxMap MapboxMap { get; private set; } = null;
    public static MainActivity MainActivityInstance { get; private set; }
    protected override void OnCreate(Bundle savedInstanceState)
    {
        [...]
        MainActivityInstance = this;
        Mapbox.GetInstance(this, "pk.###");
        MapView = new MapView(this);
        MapView.GetMapAsync(this);
        MapView.OnCreate(savedInstanceState);
    }
    protected override void OnStart()
    {
        base.OnStart();
        MapView.OnStart();
        [...]
    }
    protected override void OnResume()
    {
        base.OnResume();
        MapView.OnResume();
    }
    protected override void OnPause()
    {
        MapView.OnPause();
        base.OnPause();
    }
    protected override void OnSaveInstanceState(Bundle outState)
    {
        base.OnSaveInstanceState(outState);
        MapView.OnSaveInstanceState(outState);
    }
    protected override void OnStop()
    {
        base.OnStop();
        MapView.OnStop();
    }
    protected override void OnDestroy()
    {
        MapView.OnDestroy();
        base.OnDestroy();
    }
    public override void OnLowMemory()
    {
        base.OnLowMemory();
        MapView.OnLowMemory();
    }
    public void OnMapReady(MapboxMap p0)
    {
        MapboxMap = p0;
        [...] 
    }
    public void OnStyleLoaded(Com.Mapbox.Mapboxsdk.Maps.Style p0)
    {
        [...]
    }
}

Solution

    1. Wrap the contents of each method in try-catch, and write to Debug output any exception (hopefully instead of crashing).

    2. You will probably need to find out exactly what causes the crash, for anyone to help you.

    Do this by adding Debug.WriteLine statements throughout the custom renderer.

    1. I also recommend verifying that it really is the custom renderer that causes the problem.

    Do this by commenting out most of the custom code. So that it displays as an empty view.


    1. You need to add code to the MainActivity methods. This won't fix this crash, but it will avoid crashing later, for example when app goes into background.

    Naxam library install guide says:

    The MapView contains its own lifecycle methods for managing Android's OpenGL lifecycle, which must be called directly from the containing Activity. In order for your app to correctly call the MapView's lifecycle methods, you must override the following lifecycle methods in the Activity that contains the MapView and call the respective MapView method.

    Android code from that link:

    // Set this from your custom renderer.
    public MapView mapView;
    
    override fun onStart() {
        super.onStart()
        mapView?.onStart()
    }
     
    override fun onStop() {
        super.onStop()
        mapView?.onStop()
    }
     
    override fun onLowMemory() {
        super.onLowMemory()
        mapView?.onLowMemory()
    }
     
    override fun onDestroy() {
        super.onDestroy()
        mapView?.onDestroy()
    }
    

    Translate that into C# equivalent.


    1. In your custom renderer, set the activities mapView variable:

      Xamarin Essentials.Platform.CurrentActivity.mapView = ...;

    You should also set that variable to null:


    1. In my experience, on Android, OpenGL views don't like to be hidden - which is what happens when switching to different tab. (MapBox uses an OpenGL view.)

    Unfortunately the result is a "hard crash" - won't be caught by try/catch. Won't give useful information about why it crashed.

    If this is the cause, none of the above will fix the problem.

    You might have to do custom logic that disposes the map when the tab is hidden, recreates it when the tab comes back.

    See https://stackoverflow.com/a/52186885/199364, for the event you need to add code to.