Send a Parcelable to another App on Android

To send a parcelable to another application for it to be deserialized it is essential that:

  • the class definitions of the class to be serialized are identical in both apps,
  • the packages of the classes are identical in both apps,
  • the class loader of the receiving class is set to the ‘bundle’ before deserialization.

For the sake of consistency it might be preferable not to serialize via a parcelable at all and set the ‘extras’ to the bundle as usual. A parcelable however yields more compact code (and enforces a strict model definition).

Serialization

Bundle data = new Bundle();
data.putParcelable(KEY_MY_PARCELABLE, myParcelable);
msg.setData(data); // Message or Intent

Deserialization

Bundle data = msg.getData(); // Message or Intent
data.setClassLoader(MyParcelable.class.getClassLoader()); // Crucial to for the class to be found
MyParcelable myParcelable = data.getParcelable(KEY_MY_PARCELABLE);