Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Crash Inside MapView When Calling OnStop() Before Map is Fully Initialized #10256

Closed
simon1867 opened this issue Oct 20, 2017 · 3 comments
Closed
Labels
Android Mapbox Maps SDK for Android crash

Comments

@simon1867
Copy link

simon1867 commented Oct 20, 2017

Platform: Android
Mapbox SDK version: 5.2.0 beta 1

In my case I have a fragment that shows the user a map of where they are. On Android Api 23 or higher I have to ask the user for permission to get their location. In that scenario I don't load the map until they give me permission and I get the location. I'm running across a race condition when the user agrees to the permission I load the fragment with the map, while it in the middle of loading, I replace the fragment with another one triggered by a location update callback. OnStop() gets called on the first fragment and MapView which throws a null pointer because it hasn't finished with the async map setup.

  public void onStop() {
    mapboxMap.onStop();
    ConnectivityReceiver.instance(getContext()).deactivate();
  }

In my case mapboxMap was null.

I called onStop() from within a try catch to silence the error.
After doing that and replicating the scenario I got a crash inside of onDetachedFromWindow()

  protected void onDetachedFromWindow() {
    super.onDetachedFromWindow();
    mapZoomButtonController.setVisible(false);
  }

mapZoomButtonController was null.

I went ahead a extended MapView in which I override onDetachedFromWindow() and call the super from within a try catch.
After that, initialiseDrawingSurface() inside of MapView throws this exception:

java.lang.IllegalStateException: setRenderer has already been called for this instance.
at android.opengl.GLSurfaceView.checkRenderThreadState(GLSurfaceView.java:1934)
at android.opengl.GLSurfaceView.setEGLContextClientVersion(GLSurfaceView.java:578)
at com.mapbox.mapboxsdk.maps.MapView.initialiseDrawingSurface(MapView.java:291)
at com.mapbox.mapboxsdk.maps.MapView.access$100(MapView.java:68)
at com.mapbox.mapboxsdk.maps.MapView$1.onGlobalLayout(MapView.java:141)
at android.view.ViewTreeObserver.dispatchOnGlobalLayout(ViewTreeObserver.java:982)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2461)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1488)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7451)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:920)
at android.view.Choreographer.doCallbacks(Choreographer.java:695)
at android.view.Choreographer.doFrame(Choreographer.java:631)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:906)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7224)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

Steps to trigger behavior

  1. Navigate to an activity with a fragment that has a map
  2. Shortly after adding the fragment replace it with another fragment with a map
  3. Crash
@simon1867 simon1867 changed the title Crash Inside MapView When Stopping Activity Before Map is Fully Initialized Crash Inside MapView When Calling OnStop() Before Map is Fully Initialized Oct 20, 2017
@simon1867
Copy link
Author

I ended up preventing the crash by throttling my callback so that I don't replace my fragment before the map on the previous fragment is already loaded.

@tobrun
Copy link
Member

tobrun commented Oct 23, 2017

@simon-the-canadian thank you for the report: will look into hardening the onStop invocaton, the mapZoomButton controller has recently been fixed with:

  @Override
  @CallSuper
  protected void onDetachedFromWindow() {
    super.onDetachedFromWindow();
    if (isZoomButtonControllerInitialized()) {
      mapZoomButtonController.setVisible(false);
    }
  }

@Guardiola31337 Guardiola31337 added Android Mapbox Maps SDK for Android crash labels Oct 23, 2017
@tobrun
Copy link
Member

tobrun commented Oct 24, 2017

fixed with #10259

@tobrun tobrun closed this as completed Oct 24, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Android Mapbox Maps SDK for Android crash
Projects
None yet
Development

No branches or pull requests

3 participants