Using Picasso Singleton

I am using Picasso in my application.

First, I use only the format:

Picasso.with(context)....into(imgView); 

Thus, I assume that I am using Picasso as a singleton. I?

Secondly, I want to use setIndicatorsEnabled . However, it cannot be added to the format above, since it is not a static method. Can I use this function in the format above?

Third, if I need to create a custom instance using Picasso.Builder(...).build() to use setIndicatorsEnabled , what is the best way to achieve a one-time use in application actions?

+7
android singleton picasso
source share
2 answers

Yes, you assume that Picasso is a singleton instance when you use Picasso.with (context) ....

use on indicators

 Picasso mPicasso = Picasso.with(context); mPicasso.setIndicatorsEnabled(true); mPicasso....load().into(imageView); 

if you are using a builder, you must create your own singleton to save your Picasso instance and clean it when you are done. Do not use the builder every time you use picasso, because it creates a new instance. I believe Picasso.with (context) just takes your context and calls getApplicationContext and saves a single picasso instance with the application context.

+10
source share

Here's a good way to implement a single Picasso class

 public class ImageHandler { private static Picasso instance; public static Picasso getSharedInstance(Context context) { if(instance == null) { instance = new Picasso.Builder(context).executor(Executors.newSingleThreadExecutor()).memoryCache(Cache.NONE).indicatorsEnabled(true).build(); } return instance; } } 

And then its implementation in the code will look like this:

  ImageHandler.getSharedInstance(getApplicationContext()).load(imString).skipMemoryCache().resize(width, height).into(image, new Callback() { @Override public void onSuccess() { layout.setVisibility(View.VISIBLE); } @Override public void onError() { } }); 

Please note that you do not need to make callbacks if it is not necessary.

+3
source share

All Articles