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

[android] wrong location listener being attached after map recreation (using custom location engine) #9139

Closed

Description

@LukasPaczos

Platform: Android
Mapbox SDK version: v5.1.0-beta.3

Steps to trigger behavior

  1. Create custom engine and set it in OnMapReady() callback like this:
    mapboxMap.setLocationSource(myLocationEngine);
  2. Rotate screen

Expected behavior

App uses listener with my engine.

Actual behavior

Default LocationSource - based listener being attached instead listener tailored with my engine, which stops location updates because requestLocationUpdates() is called for wrong engine.

Notes

I took a bit of time to look into this, and this behaviour happens because when map is killed, MyLocationView#locationSource and MyLocationView#userLocationListener are being cleared in MyLocationView#onDetachedFromWindow(). Afterwards it's recreated and MyLocationView#toggleGps() creates new default LocationSource and GpsLocationListener.

We can set our custom engine only when map is delivered in onMapReady() and then we have to trigger MyLocationView#toggleGps() manually to overwrite previously created default engine, but new GpsLocationListener won't be created as there is null check in MyLocationView#toggleGps() preventing it's creation with new engine and it remains stuck with default engine, therefore requestLocationUpdates() for custom engine is not being called.

Activity

  1. added
    Android
    on May 30, 2017
  2. LukasPaczos commented on May 30, 2017

    @LukasPaczos
    ContributorAuthor

    Temporary workaround is to clear MyLocationView#userLocationListener and trigger MyLocationView#setEnabled() when we are setting new engine in MyLocationView#setLocationSource():

    public void setLocationSource(LocationEngine locationSource) { this.locationSource = locationSource; this.userLocationListener = null; setEnabled(true); }

    In general though, I think that setting custom engine should be refactored a bit to not recreate with default one if user provides a custom one.

  3. added this to the android-v5.1.0 milestone on May 30, 2017
  4. tobrun commented on May 30, 2017

    @tobrun
    Member

    I was able to reproduce the following crash with our CustomLocationEngineActivity by rotating the device:

    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.os.Handler.removeCallbacksAndMessages(java.lang.Object)' on a null object reference
                                                                                      at com.mapbox.mapboxsdk.testapp.activity.userlocation.MockLocationEngine.removeLocationUpdates(Unknown Source)
                                                                                      at com.mapbox.mapboxsdk.maps.widgets.MyLocationView.toggleGps(Unknown Source)
                                                                                      at com.mapbox.mapboxsdk.maps.widgets.MyLocationView.setEnabled(Unknown Source)
                                                                                      at com.mapbox.mapboxsdk.maps.TrackingSettings.setMyLocationEnabled(Unknown Source)
                                                                                      at com.mapbox.mapboxsdk.maps.MapboxMap.setMyLocationEnabled(Unknown Source)
                                                                                      at com.mapbox.mapboxsdk.testapp.activity.userlocation.CustomLocationEngineActivity.enableLocation(Unknown Source)
                                                                                      at com.mapbox.mapboxsdk.testapp.activity.userlocation.BaseLocationActivity.toggleGps(Unknown Source)
                                                                                      at com.mapbox.mapboxsdk.testapp.activity.userlocation.CustomLocationEngineActivity$2.onClick(Unknown Source)
                                                                                      at android.view.View.performClick(View.java:5246)
                                                                                      at android.view.View$PerformClick.run(View.java:21256)
                                                                                      at android.os.Handler.handleCallback(Handler.java:739)
                                                                                      at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                      at android.os.Looper.loop(Looper.java:145)
                                                                                      at android.app.ActivityThread.main(ActivityThread.java:7007)
                                                                                      at java.lang.reflect.Method.invoke(Native Method)
                                                                                      at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
                                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
    
    
  5. tobrun commented on May 30, 2017

    @tobrun
    Member

    There are multiple issues with this setup:

    • MyLocationView isn't able to switch correctly between multiple LocationSource instances
    • MyLocationView enabled state is restored in MapView#OnCreate before a user is able to provide it's own LocationEngine with MapboxMap#setLocationSource (this could be cleaner)
  6. LukasPaczos commented on May 30, 2017

    @LukasPaczos
    ContributorAuthor

    I can't see any easy way to resolve this issue because user can tinker with settings and provide custom content only after an instance of a map is returned. And that's too late.

  7. tobrun commented on May 30, 2017

    @tobrun
    Member

    For the former issue: we need to fixup the code so it can be switched even if another LocationSource is active (we should incorporate also deactivating the previous active location source at that time). For the latter we need to look into retaining the custom set source.

  8. LukasPaczos commented on May 30, 2017

    @LukasPaczos
    ContributorAuthor

    @tobrun alright, I pretty much got an idea how to handle that.

  9. added
    release blocker
    on Jun 7, 2017
  10. tobrun commented on Jun 8, 2017

    @tobrun
    Member

    Confirming that LOST-3.0.1-SNAPSHOT resolves the issue. PR containing this fix in #9142.

  11. added a commit that references this issue on Jun 9, 2017
  12. tobrun commented on Jun 9, 2017

    @tobrun
    Member

    Closed with #9142, thank you for all the help @LukasPaczos!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Androidrelease blocker

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions