I have an object that overwrites the Application
object. In it, I have a member variable which is a LongSparseArray
where the key is some identifier of type long
and the value is an object with 2 member variables: a Bitmap
and a long
which is used as a timestamp.
This is my global image cache. Occasionally, a function is ran that looks at the timestamps and ages things that are over an hour old.
By "age" I mean that it removes that entire entry from the LongSparseArray
.
Here is my question:
Suppose I have an Activity
with a ListView
. Each row in the ListView
has an ImageView
that is populated with an image from the cache.
Bitmap image = ((MyApp)getApplicationContext()).getImage(id);
holder.imgImage.setImageBitmap(image);
Now, suppose the user clicks some button which takes them to a new Activity
. While on this new Activity
, the image previously assigned to a row in the ListView
in the previous Activity
ages.
So, to recap, that Bitmap
key/value entry now no longer exists in the global LongSparseArray
.
Is that Bitmap
really able to be reclaimed by Java? Isn't it still being referred to by the ImageView
in the ListView
of the previous Activity
? Assuming, of course, that Android hasn't reclaimed the memory used by that Activity
.
The reason I'm asking about this is my previous aging function would also call .Recycle()
on the Bitmap
. In this scenario, when the user hit the back button and returned to the previous Activity
which was using that Bitmap
, the application would crash, presumably because that Bitmap
was not only missing from the cache, but also from memory. So I just removed the .Recycle()
call.
By the way, once the Bitmap
is removed from the cache, and an object with that id shows up on screen again, the application will download the Bitmap
again and place it in the cache. If the previous one stayed in memory, you could see how this would present a problem.
Also, does anyone have any ideas for a more effective solution?
What would happen if I set myImageView.setDrawingCacheEnabled(false);
?
There are 2 Activities
which use this image caching. One is a search screen that displays a list of items (and their images) after the user performs a search. The other is a list of those items the user has then selected to keep.