A behaviour i'm observing w.r.t passing serializable data as intent extra is quite strange, and I just wanted to clarify whether there's something I'm not missing out on.
So the thing I was trying to do is that in ActivtyA
I put a LinkedList
instance into the intent
I created for starting the next activity - ActivityB
.
LinkedList<Item> items = (some operation);
Intent intent = new Intent(this, ActivityB.class);
intent.putExtra(AppConstants.KEY_ITEMS, items);
In the onCreate
of ActivityB
, I tried to retrieve the LinkedList
extra as follows -
LinkedList<Item> items = (LinkedList<Item>) getIntent()
.getSerializableExtra(AppConstants.KEY_ITEMS);
On running this, I repeatedly got a ClassCastException
in ActivityB
, at the line above. Basically, the exception said that I was receiving an ArrayList
. Once I changed the code above to receive an ArrayList
instead, everything worked just fine.
Now I can't just figure out from the existing documentation whether this is the expected behaviour on Android when passing serializable List implementations. Or perhaps, there's something fundamentally wrong w/ what I'm doing.
Thanks.
If you are using IcePick library and are having this problem you can use Ted Hoop's technique with a custom bundler to avoid having to deal with Wrapper instances in your code.
The answer by @David Wasser is right on in terms of diagnosing the problem. This post is to share how I handled it.
The problem with any
List
object coming out as anArrayList
isn't horrible, because you can always do something likewhich will add all the elements of the deserialized list to a new
LinkedList
.The problem is much worse when it comes to
Map
, because you may have tried to serialize aLinkedHashMap
and have now lost the element ordering.Fortunately, there's a (relatively) painless way around this: define your own serializable wrapper class. You can do it for specific types or do it generically:
Then you can use this to hide your
List
,Map
, or other data type from Android's type checking:and later:
I can tell you why this is happening, but you aren't going to like it ;-)
First a bit of background information:
Extras in an
Intent
are basically an AndroidBundle
which is basically aHashMap
of key/value pairs. So when you do something likeAndroid creates a new
Bundle
for the extras and adds a map entry to theBundle
where the key isAppConstants.KEY_ITEMS
and the value is items (which is your LinkedList object).This is all fine and good, and if you were to look at the extras bundle after your code executes you will find that it contains a
LinkedList
. Now comes the interesting part...When you call
startActivity()
with the extras-containing Intent, Android needs to convert the extras from a map of key/value pairs into a byte stream. Basically it needs to serialize the Bundle. It needs to do that because it may start the activity in another process and in order to do that it needs to serialize/deserialize the objects in the Bundle so that it can recreate them in the new process. It also needs to do this because Android saves the contents of the Intent in some system tables so that it can regenerate the Intent if it needs to later.In order to serialize the
Bundle
into a byte stream, it goes through the map in the bundle and gets each key/value pair. Then it takes each "value" (which is some kind of object) and tries to determine what kind of object it is so that it can serialize it in the most efficient way. To do this, it checks the object type against a list of known object types. The list of "known object types" contains things likeInteger
,Long
,String
,Map
,Bundle
and unfortunately alsoList
. So if the object is aList
(of which there are many different kinds, includingLinkedList
) it serializes it and marks it as an object of typeList
.When the
Bundle
is deserialized, ie: when you do this:it produces an
ArrayList
for all objects in theBundle
of typeList
.There isn't really anything you can do to change this behaviour of Android. At least now you know why it does this.
Just so that you know: I actually wrote a small test program to verify this behaviour and I have looked at the source code for
Parcel.writeValue(Object v)
which is the method that gets called fromBundle
when it converts the map into a byte stream.Important Note: Since
List
is an interface this means that any class that implementsList
that you put into aBundle
will come out as anArrayList
. It is also interesting thatMap
is also in the list of "known object types" which means that no matter what kind ofMap
object you put into aBundle
(for exampleTreeMap
,SortedMap
, or any class that implements theMap
interface), you will always get aHashMap
out of it.