For `TypedArray` in Android, I added the following...
# android
c
For
TypedArray
in Android, I added the following extension function to handling parsing the values:
Copy code
fun TypedArray.parseStyledAttributes(parse: (typedArray: TypedArray) -> Unit) {
    try {
        parse(this)
    } finally {
        recycle()
    }
}
However the call-site still gives a lint warning "This typed array should be recycled after use with #recycle()". Is there anything I could do besides adding
@SuppressLint("Recycle")
everywhere?
May go with something like this for now 🤷🏻‍♂️
Copy code
@SuppressLint("Recycle")
fun Context.parseStyledAttributes(
    attributeSet: AttributeSet?,
    attributes: IntArray,
    parser: (typedArray: TypedArray) -> Unit
) {
    obtainStyledAttributes(attributeSet, attributes).parseStyledAttributes(parser)
}
a
Another option could be making use of `core-ktx`’s
TypedArray.use
extension
🙇🏻‍♂️ 1
c
🤔 actually
use
doesn't appear to remove the lint warning.
j
File a bug on lint
c
😬 if I must. Actually,
Context
has
withStyledAttributes
which calls recycle as a part of core ktx. I'm just going to stop trying to reinvent extension functions that already exist 🤦🏻
a
I had some issues with the
"Recycle"
warning in the past, but those seem to have been resolved recently with Android Studio 4.0 and the 4.0.0 Gradle plugin. Maybe it was fixed from this? https://issuetracker.google.com/issues/140344435
c
Ah, that's something to look forward to 😄