Android Question GoogleMaps App Crash Mystery

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
I wonder if anyone can help with a problem I am having.

I have an app which is using GoogleMaps within an activity.
In the activity_create I have
B4X:
  Starter.rp.CheckAndRequest(Starter.rp.PERMISSION_ACCESS_FINE_LOCATION)

    Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
    If Not(Result) Then
        Msgbox("You need to approve Location Permissions to view the map",Starter.gstrApplicationTitle)
        Activity.Finish
    End If

In the Map_ready event I have
B4X:
        gmap = mfMap.GetMap
    gmap.MyLocationEnabled = True
    gmap.MapType = gmap.MAP_TYPE_NORMAL

However, I am getting crash reports in Crashlytics which say the following:

The crash seems to suggest that the user did not approve the ACCESS_FINE_LOCATION permission, which is odd because the Activity_Create should exit.

Does anyone have any ideas why this is happening?

Thanks
 

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
Manfred that is exactly what I am doing.

In the designer, the My Location Enabled is not set.



It doesn't happen to everyone who runs the app, just on the very odd occasion.
 
Upvote 0

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
Ah, I think I have it! @DonManfred your comment gave me a clue.

In the Activity_create I have already loaded the layout, with the Map Fragment BEFORE calling CheckAndRequest.

If the User, takes a long time to decide to allow or refuse the permission, the Map_ready event is fired, whilst waiting for the PermissionResult.

Now when the code calls
gmap.MyLocationEnabled = True

in the map ready event, the permission has not been approved.

Solution:
I think I need to move the permission check to before the LoadLayout and all will be fine.

What do you think?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Now when the code calls
gmap.MyLocationEnabled = True

in the map ready event, the permission has not been approved.

Only set this property IF you HAVE permission. Use a global flag to remember. If you do not have permission; do not change the property.
 
Upvote 0

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
Yes, I will include that as well, but I think the problem was events firing out of the expected order.

Expected:
Activity Create
Application_PermissionResult
Map_ready​

Actual:
Actvity_create
Map_ready <-- this event arrives before expected because the user does not click on the permission dialog for a long time.
Application_permissionResult - Too late now!
If the user refuses the permission I close the activity.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…