Zip
Zip

Reputation: 859

How to clear images loaded by Picasso on Android?

I have a BackgroundImageActivity that load background image using Picasso. But when I go back to home (onStop() called in BackgroundImageActivity) and go into another instance of this activity, which is supposed to load another background image, but at first 2 seconds, it still shows the image from previous BackgroundImageActivity. Is this some kind of cache?

How I can clear this image so that whenever I go into a new instance of BackgroundImageActivity, i don't see the image from previous one?

public class BackgroundImageActivity extends Activity {

    @Override
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initBackground();
    }

    @Override
    protected void onStop() {
        super.onStop();
        Picasso.invalidate(imageUrl, getApplicationContext());
    }

    private void initBackground() {
        ...
    }

    private void setBg(final String imageUrl, final int bg) {

        this.imageUrl = imageUrl;

        final RequestCreator picassoRequest = Picasso.load(imageUrl, bg).memoryPolicy(MemoryPolicy.NO_CACHE);

        targetReference = new Target() {
            @Override
            public void onBitmapLoaded(final Bitmap bitmap, final Picasso.LoadedFrom from) {
                ...
            }

            @Override
            public void onBitmapFailed(final Drawable errorDrawable) {
                ...
            }

            @Override
            public void onPrepareLoad(final Drawable placeHolderDrawable) {
                ...
            }
        };

        picassoRequest.placeholder(bg).error(bg).into(targetReference);
    }


}

Thanks!!!

Upvotes: 0

Views: 1027

Answers (1)

Tabish Hussain
Tabish Hussain

Reputation: 852

Use this piece of code while loading the image.

Picasso.with(getContext())
.load(data.get(pos)
.getFeed_thumb_image())
.memoryPolicy(MemoryPolicy.NO_CACHE)
.into(image);

Picasso set the image to ImageView directly without caching in the background. This makes the app lightweight also.

Upvotes: 1

Related Questions