is it reasonable to pass a Drawable or Bitmap across via onRetainNonConfigurationInstance() to save the expense of calling res.getDrawable() again?
You should not pass drawables. Android may choose different drawables from resources after changing orientation.
In my project i am passing only data, which i loaded from internet.
http://developer.android.com/resources/articles/faster-screen-orientation-change.html
Here is written:
Be very careful with the object you pass through onRetainNonConfigurationChange(), though. If the object you pass is for some reason tied to the Activity/Context, you will leak all the views and resources of the activity. This means you should never pass a View, a Drawable, an Adapter, etc.
Sorry for my English =)
Related
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.
Issue: Once recycle() method is called on a bitmap, the bitmap should never be used again. If an attempt is made to draw the bitmap, then an exception will be thrown. From docs:
You should use recycle() only when you are sure that the bitmap is no
longer being used. If you call recycle() and later attempt to draw the
bitmap, you will get the error: "Canvas: trying to use a recycled
bitmap".
In this specific case, you have recycled the bitmap, but the ListView item's ImageView has a strong reference to the bitmap. When you return to the Activity, the ListView item attempts to draw the bitmap, hence the exception is thrown.
Bitmap memory management: Prior to Android 2.3.3, the backing pixel data of a bitmap was stored in native memory and bitmap itself in Dalvik memory. Hence to release the native memory, recycle method has to be called.
Here is Bitmap.recycle function definition:
public void recycle() {
if (!mRecycled) {
if (nativeRecycle(mNativeBitmap)) {
// return value indicates whether native pixel object was actually recycled.
// false indicates that it is still in use at the native level and these
// objects should not be collected now. They will be collected later when the
// Bitmap itself is collected.
mBuffer = null;
mNinePatchChunk = null;
}
mRecycled = true;
}
}
Post Android 3.0, the backing pixel data is also stored in Dalvik memory. When the bitmap is no longer required, we need to ensure we don't hold any strong reference to the bitmap, so that it is garbage collected.
Solution: If you are still supporting Android 2.3.3 and lower version, you still need to use recycle to release the bitmap.
You can use reference counting to track whether the bitmap is currently being referenced by the ListView item, so that even it is aged, you don't call recycle on the bitmap.
ListView adapater's getView method is the place where the bitmap is assigned to the ImageView. Here you increment the reference count. You can attach setRecyclerListener to the ListView to know whenever the listview item is put into recycle bin. This is the place you would decrement the reference count of the bitmap. The aging function need to recycle the bitmap only if the reference count is zero.
You can also consider using LruCache for caching, as mentioned in docs.
setDrawingCacheEnabled: By calling this method with true param, the next call to getDrawingCache will draw the view to a bitmap. The bitmap version of view can be rendered on to the screen. Since it is just a bitmap, we cannot interact with it as done with an actual view. Couple of use cases are:
When ListView is being scrolled, the bitmap of the displayed items view is captured and rendered. So that the views being scrolled don't undergo measure and layout pass.
View hierarchy feature in DDMS.
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 Bitmap is stilled used in the ListView (a strong reference) so dalvik can't reclaim its memory.
Apparently you can't call recycle on the Bitmap or bad things will happen(app crash, e.g.).
What would happen if I set myImageView.setDrawingCacheEnabled(false);?
If you disable drawing cache, every time your view needs to be redrawn, the onDraw method will be called.I'm not very familiar with ImageView , you can go and read its source for a deep understanding.
(Note: the usage of drawing cache is different when hardware accerleration is enabled/disabled, here I just assume you're using software rendering).
For the solution, you can try the following:
when the Bitmap cache become stale, you remove it from the cache array(and then you app will try to get a new one, I think).
In ListView.getView, you can check whether currently used Bitmap ages. It should be easy because you know the timestamp when you call setImageBitmap the first time and the latest timestamp. If they are not same, you call setImageBitmap again using the new Bitmap and the old one will be reclaimed.
Wish this helps.
Regarding, "Also, does anyone have any ideas for a more effective solution?"
The Picasso library would help solve the problems you are facing http://square.github.io/picasso/
Picasso is "A powerful image downloading and caching library for Android"
"Many common pitfalls of image loading on Android are handled automatically by Picasso:
Handling ImageView recycling and download cancelation in an adapter.
Automatic memory and disk caching."
I need to save a two dimensional array of custom object in a bundle to restore an activity after a screen rotation but I can't find an example.
You can use Bundle.putSerializable() as arrays are Serializable. You will need to make your custom class Serializable too. Here's a tutorial on implementingSerializable.
How about JSON?
Develop a method to convert your custom object into a JSON object, and add helper methods like "toJSON" & "fromJSON".
Then, you can create a JSONArray for an array of JSON Objects.
A 2D array of this object would simply be a JSONArray of JSONArrays.
Here's an example of what it would look like:
[
[
{"name":"john", "age":25},
{"name":"david", "age": 40}
],
[
{"name":"chris", "age":15},
{"name":"howard", "age":55}
]
]
Not a good idea, mate. I would suggest to save it in somewhere else rather than Bundle. You could still use the same hooker methods onSaveInstanceState & onRestoreInstanceState for the purpose. But, Bundle objects are not designed to store complex data structure. The most it should be handling is a regular object. What you are having is not even an array. It is two dimensions array. You would require a lot of parsing states just to store / restore the data.
How about you put that two dimensional arrays inside your own instance
of Application object and access it from your Activity or
something ? If the device rotates, it will only restarts the Activity.
Not the Application. So, that is one possible solution.
Another one is blocking the Activity to restart when the device rotates. The entire purpose of restarting is to use the different resource for different layout form factor. If you are not using different layout file, you probably won't need the activity to restart.
Anyway, putting complex data outside the Activity scope always helps. There are a lot of state changes and restarting and lifecycle things in Activity. Usually, if you put complex data insides Activity, it is not stable and not reliable because it keeps on changing based on Activity states.
Hope that helps.
I am using LRUCache to download images from server and showing on my activity views. I am not saving these images permanent storage(e.g. SD Card).
My Problem is that as my orientation changes, my LRUCache's object is destroyed and i am not able to get the images back after orientation change.
Before using the LRUCache i was storing the images in bundle, and hence, using onSaveInstanceState(bundle) it was easy to deal with that problem. But how to do like this with LRUCache object. Help!
Easiest way is to use a Fragment for that page. Then you can do setRetainInstance(true); to keep the OS from destroying the fragment on a config change.
Another way is to put the LruCache in an Application derived class. This is one way to keep persistent data between Activities too.
In onSaveInstanceState, try using LruCache.snapshot() to get a Map of your cache entries, ordered from least to most recently used. You could then write each key,value into the Bundle. Then when restoring from the Bundle, iterate each entry and put() them back into the LruCache.
Make a separate Class which contain a static variable holding your LRU class, your LRU will not be destroyed because it is a separate reference.
class MyLRU {
public static LruCache<String, Bitmap> mMemoryCache = new .....
}
To call the cache from your activity:
Bitmap myImage = MyLRU.mMemoryCache.get("imagekey");
I have an ArrayList of objects that each of which has a reference to a bitmap.
if I use ArrayList.remove or ArrayList.clear, to remove an object or all of them respectively,
do I need to set the reference to the bitmap to null first or does the remove/clear does it?
I ask this for memory concern, of course.
When you want to clear memory of a bitmap object you should use it recycle method.
bitmap.recycle()
Thats how the memory associated with the bitmap is cleared. As additional phone memory is used to store information of the bitmap pixel values which will be cleard using the recycle function.
After going through few articles about performance,
Not able to get this statement exactly.
"When a Drawable is attached to a view, the view is set as a callback on the drawable"
Soln: "Setting the stored drawables’ callbacks to null when the activity is destroyed."
What does that mean, e.g.
In my app , I initialize an imageButton in onCreate() like this,
imgButton= (ImageButton) findViewById(R.id.imagebtn);
At later stage, I get an image from an url, get the stream and convert that to drawable, and set image btn like this,
imgButton.setImageDrawable(drawable);
According to the above statement, when I am exiting my app, say in onDestroy()
I have to set stored drawables’ callbacks to null, not able to understand this part ! In this simple case what I have to set as null ?
I am using Android 2.2 Froyo, whether this technique is required, or not necessary.
You would have to do this only if you kept the drawable as a static field somewhere, or in a cache of some sort. In this particular situation, there's no reason to set the callback to null.
Here is exactly what was the case in example you cited:
Phone orientation has been changed, and this should mean that old activity should be "dumped" and new one created
If you have stored reference to bitmap as a static field, it has reference to old activity that was supposed to be dumped (drawable has reference to TextView, view has reference to activity)
New activity is created, but your drawable still has the reference to old one, so old one can't be dumped.
Of course, all this is right if you store drawable as static like in cited example:
private static Drawable sBackground;