https://kotlinlang.org logo
#android
Title
# android
c

Cody Engel

04/16/2020, 5:15 PM
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

Alex Vanyo

04/16/2020, 5:33 PM
Another option could be making use of `core-ktx`’s
TypedArray.use
extension
🙇🏻‍♂️ 1
c

Cody Engel

04/16/2020, 5:59 PM
🤔 actually
use
doesn't appear to remove the lint warning.
j

jw

04/16/2020, 6:09 PM
File a bug on lint
c

Cody Engel

04/16/2020, 6:13 PM
😬 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

Alex Vanyo

04/16/2020, 7:09 PM
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

Cody Engel

04/16/2020, 7:23 PM
Ah, that's something to look forward to 😄
14 Views