2016-04-14 6 views
3

Im Picasso.with(context) ..Warum in Picasso.mit (Kontext) fragt Picasso nach Kontext?

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

Und der Baumeister (Context Kontext) wie folgt

/** 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(); 
} 

Warum ist Picasso selbst für einen Zusammenhang zu fragen, wenn es immer setting context = context.getApplicationContext() ist?

Antwort

3

Sie bereits gebucht Ihre Antwort -

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

Picasso eine Bibliothek und nicht eine Anwendung. Bei der Erstellung der Picasso-Instanz, wenn Sie nicht context übergeben, dann, wie denkst du, es wird die application context von bekommen? Damit es funktioniert, benötigt es context, und es muss definitiv von der Anwendung mit dieser Bibliothek zur Verfügung gestellt werden.

+6

Es verhindert auch, dass eine "Aktivität" (wenn es das ist, was du passierst) durch einen Wechsel in den Anwendungskontext verlieren. –

+0

Vielen Dank @JakeWharton für die Klarheit! –

2

Sie brauchen nicht Kontext zu passieren nach picasso Beispiel mit Hilfe von Baumeister

// 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); 

Weitere Informationen Erstellen Sie mehr über hier lesen können: -

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