Skip to content Skip to sidebar Skip to footer

How To Cancel Picasso Requests After Fragment Transitions In Android

I have a fragment with a gridview, which loads say 20 images simultaneously using an adapter. I want to make sure that unfinished Picasso requests terminate gracefully when fragmen

Solution 1:

You have to do some thing like below, if you want to perform any operation like you asked;

Picasso.with(context)
.load("http://some.example.com")
.tag(YourTag)
.into(YourImageView)

When you want to resume then in onResume() call

picasso.resumeTag(YourTag);

When you want to pause then in onPause() call

picasso.pauseTag(YourTag);

When you want to cancel then in onStop() or in onDestroy() call

picasso.cancelTag(YourTag);

Solution 2:

Glide is very similar to Picasso (they have almost the same API) and offers lifecycle binding for request.

You just call Glide.with(fragment).load(...).into(imageView);.

Here you have nice article about differences between Glide and Picasso.

Solution 3:

I'd like to elaborate on @Akbar 's answer in that I couldn't get it to work because I had no "picasso" instance. So here's my solution. Initialize the same way, then to cancel:

Picasso.with(context).cancelRequest(YourImageView);

Bonus: if you're having issues getting the context (I know I did), or wondering what it is, you can do this in a fragment:

Contextcontext= getView().getContext();

Hope it helps

Post a Comment for "How To Cancel Picasso Requests After Fragment Transitions In Android"