One of the approaches is to implement Serializer class for each not Serializable type you wants to pass between activities. Here is a simple Serializer interface:
public interface Serializer {
void serialize(Object object, DataOutputStream out);
Object deserialize(DataInputStream in);
}
After that you can use this or any other Base64 encoder/decoder to convert DataOutputStream
to String
or other serializable type. Use for this purposes methods like:
private String serializeToString(Object object, Serializer serializer) {
ByteArrayOutputStream bStream = new ByteArrayOutputStream();
DataOutputStream dStream = new DataOutputStream(bStream);
try {
serializer.serialize(object, dStream);
} catch (IOException e) {
logger.error(e, "Couldn't serialize " + object);
return null;
}
return Base64.encodeToString(bStream.toByteArray(), false);
}
private Object deserializeFromString(String string, Serializer serializer) {
try {
return serializer.deserialize(
new DataInputStream(
new ByteArrayInputStream(
Base64.decode(string))));
} catch (IOException e) {
logger.error(e, "Couldn't deserialize [" + string + "]");
return null;
}
}
After that you can simply pass your object through activities serialized as String
and simply deserialize it back to your object when you need.