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

Accessing LocationComponent.isLocationComponentEnabled() crashes the app since 7.4.0-Snapshot #14299

Closed
carstenhag opened this issue Apr 2, 2019 · 6 comments
Labels
Android Mapbox Maps SDK for Android

Comments

@carstenhag
Copy link

carstenhag commented Apr 2, 2019

    public override fun onResume() {
        super.onResume()
        mapView.onResume()
        // After the user allowed the Location Permission in the settings or the dialog, we can enable the LocationComponent
        if (::mapboxMap.isInitialized && !mapboxMap.locationComponent.isLocationComponentEnabled && PermissionsManager.areLocationPermissionsGranted(this)) {
            enableLocationComponent()
        }
    }
Caused by: com.mapbox.mapboxsdk.location.LocationComponentNotInitializedException: The LocationComponent has to be activated with one of the LocationComponent#activateLocationComponent overloads before any other methods are invoked.
        at com.mapbox.mapboxsdk.location.LocationComponent.checkActivationState(LocationComponent.java:1521)
        at com.mapbox.mapboxsdk.location.LocationComponent.isLocationComponentEnabled(LocationComponent.java:514)
        at de.chagemann.stadtplanmosbach.MainActivity.onResume(MainActivity.kt:346)
        at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1514)
        at android.app.Activity.performResume(Activity.java:7305)

Steps to reproduce

  1. Access the isLocationComponentEnabled() method before having initialized the LocationComponent.
  2. The app crashes since 7.4.0-Snapshot

Expected behavior

  • I am allowed to know whether the component was actived yet or not
  • Needed to only enable it once
  • Needed so it can be done at arbitrary points, like in an onResume, as permissions need to be granted before initializing the component.

Actual behavior

  • Since 7.4.0-Snapshot (not sure which commit), this is disallowed.
    I am unsure as on how to activate it now.

Configuration

Android versions: Android 9
Device models: One Plus 5
Mapbox SDK versions: Works with 7.3.0 and 7.3.0-Snapshot, broken with 7.4.0

@carstenhag
Copy link
Author

carstenhag commented Apr 2, 2019

Actually, I am now using the new LocationComponentActivationOptions.Builder so that may cause the issue:

    private fun enableLocationComponent() {
        if (!PermissionsManager.areLocationPermissionsGranted(this)) {
            Log.e(TAG, "Permissions should have, but were not granted")
            return
        }

        val locationComponent = mapboxMap.locationComponent
        mapboxMap.style?.let { style ->
            val locationComponentOptions = LocationComponentOptions.builder(this).build()
            val activationOptions = LocationComponentActivationOptions.Builder(this@MainActivity, style)
                    .locationComponentOptions(locationComponentOptions)
                    .build()
            locationComponent.activateLocationComponent(activationOptions)
        }

        locationComponent.isLocationComponentEnabled = true
        locationComponent.renderMode = RenderMode.COMPASS

      // ...

    }

@tobrun tobrun added the Android Mapbox Maps SDK for Android label Apr 3, 2019
@LukasPaczos LukasPaczos added this to the release-m milestone Apr 3, 2019
@LukasPaczos
Copy link
Member

Thanks for reporting the issue @carstenhag. Per stack trace, you are checking if the component is enabled before activating it, so the crash is expected.

That said, we realize that we did not expose a proper way to check for activation state, so #14262 is on the way to address that.

Also, it's safe to activate the plugin without location permissions, those are required only when enabling it, #14311 on the way as well.

@carstenhag
Copy link
Author

So, to recap, I should proceed as following after #14311 is merged:

  1. In the onMapReady callback, activate the LocationComponent
  2. Prompting/Check the Location permission. Only activate when isLocationComponentEnabled returns false.
  3. Enable the LocationComponent

Is this right or did I get it wrong? Thanks in advance, you are very quick to answer!

@LukasPaczos
Copy link
Member

Sounds about right. You can also maintain your own flag that holds activation state if you'd like to keep current behavior (checking in the onResume) until #14262 is merged.

@LukasPaczos
Copy link
Member

#14262 has landed, closing this one.

@carstenhag
Copy link
Author

carstenhag commented Apr 8, 2019 via email

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Android Mapbox Maps SDK for Android
Projects
None yet
Development

No branches or pull requests

3 participants