1

I have an application that displays a list of photo albums and then, once an album is selected, displays photos in that album. I am using the memory cache/disk cache implementation from one of the google examples (since the photos are loaded from a website). Everything is working fine, but the disk cache initialization takes place every time an album is chosen, and the initialization takes considerable amount of time. I'd like to declare the disk cache "globally" and use it for all albums. I am not an expert in Java and not clear how to do this, particularly that various activities are being called and I can't just pass a reference to the cache when switching from one activity to another. Should the entire caching logic be build as a "service" and then "called" upon on as needed basis? Or is there a different and/or better/more elegant way of doing this?

Thank You,

Gary Kipnis
  • 722
  • 14
  • 33
  • This is the same as the question here: http://stackoverflow.com/questions/4924131/android-sharing-an-image-cache-among-activities?rq=1 – Jim Jan 04 '14 at 00:25

1 Answers1

2

Extend the Application class.

Here you are an example:

public class MyApplication extends Application {

    private static MyApplication singleInstance;
    //TODO: Your fields here

    public void onCreate() {
        super.onCreate();
        MyApplication.singleInstance = (MyApplication)getApplicationContext();
        //TODO: Your initialization code here
    }

    public static MyApplication getStaticApplicationContext() {
        return singleInstance;
    }

    //TODO: Your methods here
}

There you can add your cache and the relevant code.

You will have to reference the class into the AndroidManifest.xml file, adding the android:name attribute in the application tag, like this:

<manifest ...>
    ...
    <application ...
        android:name="com.example.app.MyApplication">
    ...
</manifest>
Juan Sánchez
  • 980
  • 2
  • 10
  • 26