• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Error retrieving a Serializable from a Bundle with a ClassLoader set

eborix13

Lurker
I'm trying to retrieve a Serializable object (Bundle.getSerializable(String key)) from a Bundle I've constructed using the Bundle(ClassLoader loader) constructor. While doing this I get a ClassNotFoundException.

The comment of the Bundle constructor states: "Constructs a new, empty Bundle that uses a specific ClassLoader for instantiating Parcelable and Serializable objects". I've looked through the source code of the Bundle class and I noticed that retrieving a Serializable object executes the Bundle.unparcel() method which finally comes down to the Parcel.readValue(ClassLoader loader). All good so far. But the readValue method doesn't use the ClassLoader to read Serializable objects:

Code:
 case VAL_SERIALIZABLE:
            return readSerializable();
as it does when reading Parcelable objects:

Code:
 case VAL_PARCELABLE:
            return readParcelable(loader);
This seems to be the cause of the error I get.

Is the comment of the Bundle(ClassLoader loader) constructor correct and the reading of the Serializable object is a bug? Or is the code in the Parcel.readValue() method correct and the comment is wrong? The comment of the Bundle.setClassLoader(ClassLoader loader) method is a bit more vague in relation to Serializable objects: "Changes the ClassLoader this Bundle uses when instantiating objects", although it basically sets the same ClassLoader that is set in the constructor. So, are Serializable objects retrieved using the specified ClassLoader or not?
 
Back
Top Bottom