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.
Kiki Abdullah
02/27/2022, 4:47 AM
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
zsmb
02/27/2022, 11:29 AM
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
Kiki Abdullah
02/27/2022, 11:37 AM
That means I have to call
userAvatar.recycle()
inside the for loop just after
listUser.add(user)
statement?
z
zsmb
02/27/2022, 1:11 PM
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.