Question

When my application connects to the server I check which icons are outdated or missing.
I store those icons in a sqllite database, the icons aren't very big, around between 5-10kb.

Everytime an activity starts, I'll go to the database and get the necessary icons.

But many of my activities use the same icons, so I was thinking about getting them from the database once, and then cache them somewhere to speed up my application.

How should I do this ?

I was reading this link: http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html and they mentioned an lruCache for bitmaps.

But isn't this a bit of overkill for what I'm trying to achieve?

Was it helpful?

Solution

Absolutely no. The LruCache is the proper approach to cache stuff, it doesn't matter the size of the images. Remember to create the cache as a singleton element to be accessed equally by any element on your whole application.

DO NOT USE WeakReference as some people will suggest. They do not work very well on the Android, if you're in a search mood you can check the Google IO 2012 talks on YouTube that the Android team themselves are explaining why not use WeakReference and how to properly use LruCache.

edit

found the youtube link http://www.youtube.com/watch?v=gbQb1PVjfqM&feature=plcp

OTHER TIPS

Your question does not sound like you are asking how to do a memory cache, but whether it's reasonable to do it at all. Does the application already feel like the icon loading is a bottleneck? What else is your application doing? Some networking perhaps? How many activities (out of all activities) actually do have some icons? How many icons per activity do you have? How many icons is there in total? Guessing from @Budius post, doing memory cache right is not completely straightforward issue on Android, so make sure that you are not doing an unjustified premature optimization

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top