问题
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 fragment has disappeared/disposed.
Question
- Is this automatically handled by Picasso framework (so I don't have to worry)
- How to implement cancel all Picasso requests on dispose?
回答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);
回答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.
回答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:
Context context = getView().getContext();
Hope it helps
来源:https://stackoverflow.com/questions/32629979/how-to-cancel-picasso-requests-after-fragment-transitions-in-android