Passing complex data between two activities via byte serialization (Google protobuf)
UPDATE: A much better way for passing data between Activities is to use the Parcelable interface. Use protobuf if you want to serialize an object without using heavy Java serialization.
First time android developer frequently asked question: How to pass complex data from one activity to another? One exotic example is to use XML string serialization. Marshalling is performed at the originating activity and demarshalling is performed at the target activity. One drawback with using XML is you have more extra bytes (caused by unnecessary tags) which leads to more memory usage.
An alternative approach here is to use Google protobuf to serialize my data into a byte array and then put that in the bundle along with the intent.
1. The java class model is generated from the proto file.
2: At the originating activity, a builder for the model is instanced. After setting properties for the model object, calling build() method returns instance A. To serialize the model, just call the toByteArray() method of object A. The byte array is included in the bundle to sent along with the fired intent.
3: Reverse the process at the
Since this method uses the Google protobuf library to do the byte serialization you need to consider if an extra 300kb to the final application size is acceptable.
The model class is generated from the .proto file, which is a protobuf descriptor file.