How to cancel Picasso requests after fragment transitions in android

橙三吉。 提交于 2019-12-12 03:59:36

问题


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

  1. Is this automatically handled by Picasso framework (so I don't have to worry)
  2. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!