Suraj Kumar Sau
Suraj Kumar Sau

Reputation: 458

Why in Picasso.with(context), Picasso asks for context?

In the Picasso.with(context) ..

public static Picasso with(Context context) {
  if (singleton == null) {
    synchronized (Picasso.class) {
      if (singleton == null) {
        singleton = new Builder(context).build();
      }
    }
  }
  return singleton;
}

And the Builder(Context context) like this

/** Start building a new {@link Picasso} instance. */
public Builder(Context context) {
  if (context == null) {
    throw new IllegalArgumentException("Context must not be null.");
  }
  this.context = context.getApplicationContext();
}

Why is Picasso even asking for a context when it is always setting context = context.getApplicationContext( ) ?

Upvotes: 3

Views: 3948

Answers (2)

Hitesh Sahu
Hitesh Sahu

Reputation: 45072

You do not need to pass context after creating picasso instance with the help of builder

    // create Picasso.Builder object
    Picasso.Builder picassoBuilder = new Picasso.Builder(context);

    // Picasso.Builder creates the Picasso object to do the actual requests
    Picasso picasso = picassoBuilder.build(); 

    // instead of Picasso.with(Context context) you directly use this new custom Picasso object

picasso  
    .load(UsageExampleListViewAdapter.eatFoodyImages[0])
    .into(imageView1);

For more information you can read more about it here :-

https://futurestud.io/blog/picasso-customizing-picasso-with-picasso-builder

Upvotes: 4

Shadab Ansari
Shadab Ansari

Reputation: 7070

You already posted your answer -

public Builder(Context context) {
  if (context == null) {
    throw new IllegalArgumentException("Context must not be null.");
  }
  this.context = context.getApplicationContext();
}

Picasso is a library and not an application. While creating Picasso instance if you'll not pass context, then how do you think it will get the application context from ? For it to work it requires context , and it definitely needs to be provided by the application using this library.

Upvotes: 3

Related Questions