I got this warning `This `TypedArray` should be re...
# android
k
I got this warning `This
TypedArray
should be recycled after use with `#recycle()`` after use
obtainTypedArray
for my array list. But when I try to call
recycle()
,
getResourceId()
show
unresolved reference
error.
This is my code:
Copy code
private val listUsers: ArrayList<User>
        get() {
            val userName = resources.getStringArray(R.array.name)
            val userUserName = resources.getStringArray((R.array.username))
            val userAvatar = resources.obtainTypedArray(R.array.avatar)
            val listUser = ArrayList<User>()
            for (i in userName.indices) {
                val user = User(userName[i], userUserName[i], userAvatar.getResourceId(i, -1))
                listUser.add(user)
            }
            return listUser
        }
z
The
obtainTypedArray
method returns a
TypedArray
, which has to be recycled when you no longer need it. In your case, this means that you should call
userAvatar.recycle()
once you're done using that object - probably between the
for
loop and the
return
statement.
k
That means I have to call
userAvatar.recycle()
inside the for loop just after
listUser.add(user)
statement?
z
Only call
recycle
after you're completely done using the array. If you use it in the for loop on each iteration, call it after the loop is done.
141 Views