开发者

pass a protocol buffers object by an Android Intent

Any Serializable or Parceable objects can be exchanged by Intent. But protobuf objects do not implement Serializable or Parceable. Since protobuf objects implementation may change in the future, it's not advised to modify/extend them.

How to exchange by Android Intent data f开发者_运维问答rom protocol buffer message (generated class) ?

EDIT : I was using protobuf-2.3.0 where GeneratedMessage and GeneratedMessageLite don't implement Serializable. Generated messages started to implement Serializable on 2 November 2010. Protobuf-2.4.1 was released on April 2011.


The following answer was true in 2011, but GeneratedMessageLite no longer implements Serializable. GeneratedMessage still does.

GeneratedMessage and GeneratedMessageLite both implement Serializable, so you should be able to just serialize any specific generated message class as far as I'm aware. Were you trying to serialize just Message by any chance?


For now probably the best way to do this in Java-lite (currently recommended for Android) is convert the object to ByteArray and then send it to the Activity and convert the byte Array back to the object in the target activity.

// convert to byte array and pass to intent

Intent listResults = new Intent(activity, ImageResults.class);

listResults.putExtra( "reply", reply.toByteArray());


// Convert byte array to Object
 result_list = (Search.SearchResponse) Search.SearchResponse.parseFrom(getIntent().getByteArrayExtra("reply"));

Java-nano used to implement Parcelable which could be used with Android Parcelable to send objects between Activities. However Java-nano is no longer, and GeneratedMessageLite doesn't implement either Serializable or Parcelable.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜