onBitmapLoaded of Target object not called on first load

As noted by the other respondents (@lukas and @mradzinski), Picasso only keeps a weak reference to the Target object. While you can store a strong reference Target in one of your classes, this can still be problematic if the Target references a View in any manner, since you’ll effectively also be keeping a strong reference to that View as well (which is one of the things that Picasso explicitly helps you avoid).

If you are in this situation, I’d recommend tagging the Target to the View:

final ImageView imageView = ... // The view Picasso is loading an image into
final Target target = new Target{...};
imageView.setTag(target);

This approach has the benefit of letting Picasso handle everything for you. It will manage the WeakReference objects for each of your views – as soon as one is no longer needed, whatever Target processing the image will also be released, so you’re not stuck with memory leaks due to long-lived targets, but your Target will last as long as its view is alive.

Leave a Comment