You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Aug 8, 2023. It is now read-only.
Repository navigation
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
Create custom engine and set it in OnMapReady() callback like this: mapboxMap.setLocationSource(myLocationEngine);
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.
Temporary workaround is to clear MyLocationView#userLocationListener and trigger MyLocationView#setEnabled() when we are setting new engine in MyLocationView#setLocationSource():
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)
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)
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.
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.
Activity
LukasPaczos commentedon May 30, 2017
Temporary workaround is to clear
MyLocationView#userLocationListenerand triggerMyLocationView#setEnabled()when we are setting new engine inMyLocationView#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.
tobrun commentedon May 30, 2017
I was able to reproduce the following crash with our CustomLocationEngineActivity by rotating the device:
tobrun commentedon May 30, 2017
There are multiple issues with this setup:
MyLocationViewisn't able to switch correctly between multipleLocationSourceinstancesMyLocationViewenabled state is restored inMapView#OnCreatebefore a user is able to provide it's ownLocationEnginewithMapboxMap#setLocationSource(this could be cleaner)LukasPaczos commentedon May 30, 2017
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.
tobrun commentedon May 30, 2017
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.
LukasPaczos commentedon May 30, 2017
@tobrun alright, I pretty much got an idea how to handle that.
[android] - custom location engine fixes (#9139)
[android] - custom location engine fixes (#9139)
tobrun commentedon Jun 8, 2017
Confirming that LOST-3.0.1-SNAPSHOT resolves the issue. PR containing this fix in #9142.
Custom location source fix (#9142)
tobrun commentedon Jun 9, 2017
Closed with #9142, thank you for all the help @LukasPaczos!
Merge release-ios-v3.6.0-android-v5.1.0 (#9416)