Skip to content Skip to sidebar Skip to footer

Picasso: How To Check If Singleton Is Already Set

I am using Picasso for handling image loading in my app. In my Activity I do @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

Solution 1:

Call your setupPicasso() method from onCreate in your own Application class. Extend the Application class and override onCreate. Then you will need to add the name attribute to your manifest's <application> section with the fully qualified application class.

package my.package

public class MyApplication extends Application {

@Override
public void onCreate() {
    super.onCreate();
    setupPicasso();
}

Then in your manifest,

<application 
  android:name="my.package.MyApplication"
... >

Solution 2:

You will have to extend the application class like iagreen mentioned but you should also surround the setUpPicasso method with a try/catch so that if the exception is thrown you can handle it and prevent your application from crashing.

so

package my.package

public class MyApplication extends Application {

@Override
public void onCreate() 
{
    super.onCreate();

    try
    {
        setupPicasso();
    }

    catch ( IllegalStateException e )
    {
        //TODO
    }
}

Solution 3:

You could also try this.

private static boolean initializedPicasso = false;


if (!initializedPicasso) {
     setupPicasso();
     initializedPicasso = true;
}

Solution 4:

Surround the setupPicasso() method in a try-catch like so:

try
{
    setupPicasso();
} 
catch (Exception e) 
{
    e.printStackTrace();
}

Post a Comment for "Picasso: How To Check If Singleton Is Already Set"