anthony
anthony

Reputation: 7733

Android: Is it a good practice to clear Glide manually in Adapter (RecyclerView) ?

I have a Recyclerview which manages a newsfeed. There are lot of images. With Android monitor, when I scroll in this list, the memory allocated increases always ! So I added in these 2 override methods (in my Adapter):

@Override
public void onViewDetachedFromWindow(RecyclerView.ViewHolder holder) {
    super.onViewDetachedFromWindow(holder);
    clearAdapter(holder);
}

@Override
public void onViewRecycled(RecyclerView.ViewHolder holder) {
    super.onViewRecycled(holder);
    clearAdapter(holder);
}

protected void clearAdapter(RecyclerView.ViewHolder holder) {
    Glide.clear(holder.mImageView);
    ...
}

The result is better about memory allocated (but not perfect!). AND now I have a new problem, because sometimes some images are not loaded in my newsfeed (it's completely random!)

Thanks for your help guys!

Upvotes: 5

Views: 2607

Answers (1)

Sam Judd
Sam Judd

Reputation: 7387

Calling clear in onViewRecycled should be fine. It can save memory and improve Bitmap re-use if you have a number of views in the recycled view pool (which you almost always do).

However, calling clear in onViewDetachedFromWindow is probably not fine. It's possible a view may be detached and then re-attached without being re-bound. If that happens, the view may appear empty.

Try removing the call to clearAdapter in onViewDetachedFromWindow and see if that helps.

Upvotes: 9

Related Questions