Search code examples
androidpicasso

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( ) ?


Solution

  • 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.