Take the 2-minute tour ×

I am developing an Android application and can't quite figure out the best way of implementing a 2-level image cache that can be shared among multiple activities w/in a single application.


Application has 3 activities (A, B, and C) and for the sake of argument lets say that A calls B and B calls C and C calls A. Each activity displays an image downloaded from the web and I'm using asynctask to download and display images w/in each activity - easy enough. Now I'd like to add an image cache to avoid multiple downloads of the same image.

Right now each activity starts a new instance of a simple asynctask that downloads the image and updates the view appropriately. Obviously its easy enough to update the basic asynctask to check the image cache before proceeding to download and to update the cache once the download is complete but I'm stuck on how/where to create and initialize the cache. Any thoughts would be appreciated.

share|improve this question
why not put the images on the sd card? –  androidworkz Feb 7 '11 at 18:15
add comment

1 Answer

You can add this to your app's manifest:

application android:name ="MyApplication" (...)

You can create then an class that has the name "MyApplication". You can then use that class across your activities. Checking, before making the asynk call, if you already have a proper image to use. If you have, you use the one "cached", if not, you can get a new one. You can try something like this (in this case to get some random strings):

ArrayList myStrings = ((MyApplication) this.getApplication()).getRandomStrings();

Hope this helped you. :)

Edit: Dont forget to the create your "MyApplication" like this:

public class MyApplication extends Application

share|improve this answer
Thanks. The key was to extend the application task. –  tsr Feb 9 '11 at 4:14
If you acepted my answer, please mark it. :) –  Jacky Boy Feb 9 '11 at 11:58
add comment

Your Answer


