what is difference between Parcelable and Serialization used in android

后端 未结 5 893
北恋
北恋 2020-12-14 06:33

I want to know exact ,

  1. whether should I used parcelable or serialization technique for sending data from one activity to other?
5条回答
  •  情歌与酒
    2020-12-14 07:15

    both parceling and serializing are ways to marshall and unmarshal data. in android this is used to pass non-primitive data types between components and processes. in general, android allows either serializable or parcelable objects, so you can choose your method. the exception to that is with AIDL interfaces. objects must be parcelable to be passed / returned.

    serialization uses reflection to automatically marshall and unmarshal data. in most cases implementing the marker interface is enough to make it just work. with parceling, you have to write the code to marshall and unmarshal the data yourself.

    and hence, that is why parceling is faster. the object does not need to be reflected to discover the fields. it's the reflection that makes it slow.

    serialization also has in-built versioning ... if you try to unmarshal to a different version of the object's class that was marshalled, the process will fail in a predictable way. with parceling, you can do the same thing but you need to implement it yourself by adding a "version field to your object, and code that checks the version when unmarhsaling.

    that being said, i typically use serialization. with simple objects you won't notice the difference. you can always change to use parceling later in development if you discover performance issues.

提交回复
热议问题