使用Parcelable,而不是序列化对象的利益而不是、对象、利益、序列化

2023-09-11 11:11:36 作者:我再不好有人爱就好

据我了解,捆绑 Parcelable 属于Android的中进行序列化的方式。它用于例如在活动之间传递数据。但是我在想,如果在的情况下救了我的业务对象的状态到内部存储器例如使用 Parcelable 而不是经典的系列化什么好处?它会比传统方式更简单或更快?我应该在哪里使用传统的序列化和更好的地方使用包?

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?

推荐答案

从专业的Andr​​oid 2

From "Pro Android 2"

请注意:看到Parcelable可能引发的问题,为什么机器人不能使用       内置Java序列化机制?事实证明,在   Android团队得出的结论       在Java中的序列化实在太慢,以满足Android的   进程间通信       要求。因此,团队构建的Parcelable解决方案。该   Parcelable方式要求       您明确序列化类的成员,但最终,   你会得到一个更快       序列化的对象。

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.

也意识到,Android提供了两种机制,允许您通过   数据传输到另一个       处理。第一是使用意图束传递到活动中,   第二个是通过一个       Parcelable到服务。这两种机制不能互换和   不应该是       困惑。即,Parcelable并不意味着被传递到一个   活动。如果你想开始       一个活动,并通过它的一些数据,使用一个包。 Parcelable意指   仅作为一部分       一个AIDL定义。

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.