Avoid crash on Android 4 by getting a vector icon from the local context instead of the application context. #3247
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Issues: Closes #3246.
Scope: MapboxMapFragment
Requested reviewers: @lognaturel
User-visible changes
A crash when opening a Mapbox map on Android 4.x is avoided.
Internal changes
I was able to replicate and confirm this crash in an Android 4.x emulator. I was puzzled by this, and don't understand why it's happening.
First, I tried switching to
VectorDrawableCompat.create()
as recommended by this Stack Overflow post. This did avoid the crash.But then I went looking and found that there are many places where we call
ContextCompat.getDrawable
to get a vector icon (e.g.FileArrayAdapter
,FormHierarchyActivity
), and when I exercised those code paths in my Android 4.4 emulator, they did not crash. There is even a similar call inOsmDroidMapFragment
and it does not crash.Upon closer inspection I found that these other calls did not use
getApplicationContext()
, but instead got a context from a local Activity or View. So I tried switchingMapboxMapFragment
to also use the fragment's context instead of the application context, and that seems to work.I don't understand why it works, though.