28

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.

anirvan
  • 4,797
  • 4
  • 32
  • 42
  • but is there any particular reason why for `LinkedList` this behaviour occurs, whereas, if i were to have added an `ArrayList` instance as extra data on the `intent` everything would be fine. And, i won't need to use `Parcelable`? – anirvan Sep 06 '12 at 15:42
  • +1 for the interesting question. I spent some time thinking about this and was so intrigued I went to figure it out for myself. Now you and I are both smarter (see my answer). – David Wasser Sep 06 '12 at 17:37
  • @anirvan You can't pass an `ArrayList` using the you way you have done. Only raw type of ArrayList is allowed that is `String` or `Integer` – Lalit Poptani Sep 07 '12 at 04:32
  • @LalitPoptani you're mistaken. As long as the `CustomObject` implements `Serializable` there's nothing wrong in doing so. And it works just fine. – anirvan Sep 07 '12 at 05:13
  • @anirvan yes you are right, but you have to implement `Serializable` without which I won't be possible. – Lalit Poptani Sep 07 '12 at 05:16
  • I f you are just wanting to use Serializable then you create a class with getter setter of LinkedList and implement Serializable and pass it. – Lalit Poptani Sep 07 '12 at 05:17
  • @LalitPoptani that would certainly be quite an overkill. Besides, that's not the point of my asking the question. It's more about understanding the behaviour. There can be *many* hacks. David's answer pretty much nails it. – anirvan Sep 07 '12 at 05:23

3 Answers3

53

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 Android Bundle which is basically a HashMap of key/value pairs. So when you do something like

intent.putExtra(AppConstants.KEY_ITEMS, items);

Android creates a new Bundle for the extras and adds a map entry to the Bundle where the key is AppConstants.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 like Integer, Long, String, Map, Bundle and unfortunately also List. So if the object is a List (of which there are many different kinds, including LinkedList) it serializes it and marks it as an object of type List.

When the Bundle is deserialized, ie: when you do this:

LinkedList<Item> items = (LinkedList<Item>)
        getIntent().getSerializableExtra(AppConstants.KEY_ITEMS);

it produces an ArrayList for all objects in the Bundle of type List.

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 from Bundle when it converts the map into a byte stream.

Important Note: Since List is an interface this means that any class that implements List that you put into a Bundle will come out as an ArrayList. It is also interesting that Map is also in the list of "known object types" which means that no matter what kind of Map object you put into a Bundle (for example TreeMap, SortedMap, or any class that implements the Map interface), you will always get a HashMap out of it.

David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • 3
    My, oh my - might I first say that *yes, I really don't like this*. After all, the whole concept of "*known object types*" sounds like some one wanted to cut corners when building that part of the logic. And also, if this be the case, they should've actually made it a point to prevent all "*not-so-known*" object types from implementing `Serializable`, or, not supporting `Serializable` at all within intent `extras`. Anyways, I can't thank you enough, and yes, we're much the smarter for what you've found. – anirvan Sep 07 '12 at 05:20
  • 3
    Thanks a lot for the answer. Saved me hours of debugging. – Andree Feb 11 '13 at 12:54
  • 2
    Thanks David, this saved me... I had the same issue with savedInstanceState that was passed to my fragment - and I could not figure out why Android insisted on giving me an ArrayList... – Patrick Dec 06 '13 at 06:38
5

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 an ArrayList isn't horrible, because you can always do something like

LinkedList<String> items = new LinkedList<>(
    (List<String>) intent.getSerializableExtra(KEY));

which 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 a LinkedHashMap 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:

public class Wrapper <T extends Serializable> implements Serializable {
    private T wrapped;

    public Wrapper(T wrapped) {
        this.wrapped = wrapped;
    }

    public T get() {
        return wrapped;
    }
}

Then you can use this to hide your List, Map, or other data type from Android's type checking:

intent.putExtra(KEY, new Wrapper<>(items));

and later:

items = ((Wrapper<LinkedList<String>>) intent.getSerializableExtra(KEY)).get();
Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
0

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.

public class LinkedHashmapBundler implements Bundler<LinkedHashMap> {

 @Override
 public void put(String s, LinkedHashMap val, Bundle bundle) {
    bundle.putSerializable(s, new Wrapper<>(val));
 }

 @SuppressWarnings("unchecked")
 @Override
 public LinkedHashMap get(String s, Bundle bundle) {
   return ((Wrapper<LinkedHashMap>) bundle.getSerializable(s)).get();
 }
} 

// Use it like this
@State(LinkedHashmapBundler.class) LinkedHasMap map
Sergio Serra
  • 1,479
  • 3
  • 17
  • 25