Benefit of using Parcelable instead of serializing

2019-01-01 03:50发布

As I understand, Bundle and Parcelable belongs to the way Android performs serialization in. It is used for example in passing data between activities. But I wonder, if there are any benefits in using Parcelable instead of classic serialization in case of saving state of my business objects to the internal memory for example? Will it be simpler or faster than the classic way? Where should I use classic serialization and where better to use bundles?

9条回答
有味是清欢
2楼-- · 2019-01-01 03:54

Based on this article http://www.mooproductions.org/node/6?page=5 Parcelable should be faster.

Not mentioned in the article, is that I don't htink that serializable objects will work in AIDL for remote services.

查看更多
孤独寂梦人
3楼-- · 2019-01-01 04:02

I just use GSON -> Serialise to JSON String -> Restore Object from JSON String.

查看更多
浪荡孟婆
4楼-- · 2019-01-01 04:03

From "Pro Android 2"

NOTE: Seeing Parcelable might have triggered the question, why is Android not using the built-in Java serialization mechanism? It turns out that the Android team came to the conclusion that the serialization in Java is far too slow to satisfy Android’s interprocess-communication requirements. So the team built the Parcelable solution. The Parcelable approach requires that you explicitly serialize the members of your class, but in the end, you get a much faster serialization of your objects.

Also realize that Android provides two mechanisms that allow you to pass data to another process. The first is to pass a bundle to an activity using an intent, and the second is to pass a Parcelable to a service. These two mechanisms are not interchangeable and should not be confused. That is, the Parcelable is not meant to be passed to an activity. If you want to start an activity and pass it some data, use a bundle. Parcelable is meant to be used only as part of an AIDL definition.

查看更多
余生请多指教
5楼-- · 2019-01-01 04:05

It seems that nowadays the difference isn't so noticeble, at least not when you run it between your own activities.

According to tests shown on this website , Parcelable is about 10 times faster on newest devices (like nexus 10), and is about 17 faster on old ones (like desire Z)

so it's up to you to decide if it worths it.

maybe for relatively small and simple classes, Serializable is fine, and for the rest, you should use Parcelable

查看更多
一个人的天荒地老
6楼-- · 2019-01-01 04:12

Parcelable is mainly related to IPC using the Binder infrastructure, where data is passed as Parcels.

Since Android relies a lot on Binder for most, if not all, IPC tasks, it makes sense to implement Parcelable in most places, and especially in the framework, because it allows pass an object to another process if you need that. It makes objects "transportable".

But if you have a non Android-specific business layer which extensively use serializables to save object states, and only need to store them to the file system, then I think that serializable is fine. It allows to avoid the Parcelable boiler-plate code.

查看更多
还给你的自由
7楼-- · 2019-01-01 04:12

Also Parcelable offers custom implementation where the user gets an chance to parcel each of his objects by overriding the writeToParcel(), However serialization does not this custom implementation as its way of passing data involves JAVA reflection API.

查看更多
登录 后发表回答