简体   繁体   中英

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)
        {
            var view = e.NewElement as Map;
            _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.

  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:

  • in OnElementChanged / e.newElement == null
  • in Dispose.

  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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM