使用 Parcelable 而不是序列化对象的好处 [英] Benefit of using Parcelable instead of serializing object

查看:29
本文介绍了使用 Parcelable 而不是序列化对象的好处的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,BundleParcelable 属于 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?

推荐答案

来自Pro Android 2"

From "Pro Android 2"

注意:看到 Parcelable 可能触发了这个问题,为什么 Android 不使用内置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 是为了仅用作的一部分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.

这篇关于使用 Parcelable 而不是序列化对象的好处的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆