序列化之Parcelable
序列化主要是用来传递类的信息,一般java有提供serializable类,这个类用的较多,不过在android上面似乎效率不高,于是google开发了针对性的优化的接口,parcelable接口。
从名字上来看,parcel是包裹的意思,传递用包裹来表示,比较形象。不过这个接口以及文档,写的比较抽象,stackoverflow上面的人说肯定是C++程序员写的、、、所以广大Java程序员需要耐着性子看。
首先,我们需要实现接口Parcelable。
然后,有几个方法需要重写,
describeContents()这个按照默认,我们通常返回0,据文档上面说是FileDescriptor,没有搞太懂。
Describe the kinds of special objects contained in this Parcelable's
* marshalled representation.
*
* @return a bitmask indicating the set of special object types marshalled
* by the Parcelable.
writeToParcel(Parcel dest,int flags) ,把这个对象放进Parcel中。第一个参数是应该被写入的parcel,第二个参数一般我们不怎么用。
/**
* Flatten this object in to a Parcel.
*
* @param dest The Parcel in which the object should be written.
* @param flags Additional flags about how the object should be written.
* May be 0 or {@link #PARCELABLE_WRITE_RETURN_VALUE}.
*/
public void writeToParcel(Parcel dest, int flags);
另外需要注意一个Creator,只有实现了这个接口,parcel常量类才可以将其parcelable。
这个接口有两个方法要重写,
createFromParcel,这个用来创建一个parcel对象的实例。
/**
* Interface that must be implemented and provided as a public CREATOR
* field that generates instances of your Parcelable class from a Parcel.
*/
public interface Creator<T> {
/**
* Create a new instance of the Parcelable class, instantiating it
* from the given Parcel whose data had previously been written by
* {@link Parcelable#writeToParcel Parcelable.writeToParcel()}.
*
* @param source The Parcel to read the object's data from.
* @return Returns a new instance of the Parcelable class.
*/
public T createFromParcel(Parcel source); /**
* Create a new array of the Parcelable class.
*
* @param size Size of the array.
* @return Returns an array of the Parcelable class, with every entry
* initialized to null.
*/
public T[] newArray(int size);
}
下面这个是Google提供的官方doc。
public class MyParcelable implements Parcelable {
* private int mData;
*
* public int describeContents() {
* return 0;
* }
*
* public void writeToParcel(Parcel out, int flags) {
* out.writeInt(mData);
* }
*
* public static final Parcelable.Creator<MyParcelable> CREATOR
* = new Parcelable.Creator<MyParcelable>() {
* public MyParcelable createFromParcel(Parcel in) {
* return new MyParcelable(in);
* }
*
* public MyParcelable[] newArray(int size) {
* return new MyParcelable[size];
* }
* };
*
* private MyParcelable(Parcel in) {
* mData = in.readInt();
* }
序列化之Parcelable的更多相关文章
- 安卓实现序列化之Parcelable接口
安卓实现序列化之Parcelable接口 1.实现序列化的方法: Android中实现序列化有两个选择:一是实现Serializable接口(是JavaSE本身就支持的) .一是实现Parcelabl ...
- android 很详细的序列化过程Parcelable
直接上代码:注释都写的很清楚了. public class Entry implements Parcelable{ public int userID; public String username ...
- Android为TV端助力 很详细的序列化过程Parcelable
直接上代码:注释都写的很清楚了. public class Entry implements Parcelable{ public int userID; public String username ...
- Android序列化:Parcelable/Serializable
1.易用性及速度 1.1 Serializable——简单易用 Serializable的作用是为了保存对象的属性到本地文件.数据库.网络流.rmi以方便数据传输,当然这种传输可以是程序内的也可以是两 ...
- android序列化(1)Parcelable与Serializable
1.Android中实现序列化有两个选择 一是实现Serializable接口(是JavaSE本身就支持的),实现Serializable接口非常简单. 一是实现Parcelable接口(是Andro ...
- android序列化(2)Parcelable与Parcel
1.简介 Parcel : 包裹 Android采用这个它封装消息数据.这个是通过IBinder通信的消息的载体.需要明确的是Parcel用来存放数据的是内存(RAM),而不是永久性介质(Nand等 ...
- 序列化Serializable和Parcelable
版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 简单记录下序列化Serializable和Parcelable的使用方法. Android中Intent如果要传递类对象,可以通过两 ...
- 【Android - IPC】之Serializable和Parcelable序列化
1.序列化的目的 (1)永久的保存对象数据(将对象数据保存到文件或磁盘中): (2)通过序列化操作将对象数据在网络上进行传输(由于网络传输是以字节流的方式对数据进行传输的,因此序列化的目的是将对象数据 ...
- Parcelable和Serializable的区别
一.Android为什么要序列化?什么是序列化,怎么进行序列化 why 为什么要了解序列化?—— 进行Android开发的时候,无法将对象的引用传给Activities或者Fragments,我们 ...
随机推荐
- CSS3/SVG clip-path路径剪裁遮罩属性简介
一.SVG属性和CSS3属性千丝万缕的关系 CSS3新增属性除了我们现在用的比较多的border-radius, box-shadow, gradient, ...之类,还有很重要的一个分支:SVG属 ...
- 016专家视图测试脚本开发- Environment对象的使用;Msgbox和Print的区别
(1)读取Built-in 'file -> settings -> Enrionment -> variable type 为 Built-in os = Environment. ...
- VS2008压力测试时web测试记录器无显示
系统:win7 浏览器:IE8 web测试记录器:Web Test Recorder 在运行vs2008Web压力测试时一直在浏览器左侧的web测试记录器无显示. 解决办法: 在IE工具栏 ...
- javaweb之javascript结合(三)
1.案例一:在末尾添加节点 第一步:获取到ul标签 第二步:创建li标签 document.createElement("标签名称")方法 第三步:创建文本 document.cr ...
- C++之内部类(嵌套类)与外部类及友元
本人能力.精力有限,所言所感都基于自身的实践和有限的阅读.查阅,如有错误,欢迎拍砖,敬请赐教——博客园:钱智慧. 先上代码: class Outer { public: Outer(){m_outer ...
- Sublime Text2使用心得
代码编辑器或者文本编辑器,对于程序员来说,就像剑与战士一样,谁都想拥有一把可以随心驾驭且锋利无比的宝剑,而每一位程序员,同样会去追求最适合自己的强大.灵活的编辑器,相信你和我一样,都不会例外. 我用过 ...
- Android ListView 全面优化
结合昨天学习的多线程,今天又继续对ListView进行了优化,包括异步加载图片,滑动时暂停加载,滑动停止后再加载显示界面中的item. 综合ListView在使用时参考的多篇博客,这里对ListVie ...
- 归并排序,递归法,C语言实现。
利用归并排序法对序列排序的示意图(递归法): 一.算法分析:利用递归的分治方法:1.将原序列细分,直到成为单个元素:2.在将分割后的序列一层一层地按顺序合并,完成排序.细分通过不断深入递归完成,合并通 ...
- A Tour of Go Struct Fields
Struct fields are accessed using a dot. package main import "fmt" type Vertex struct { X i ...
- ASP.NET- Repeater 嵌套
我们有时候需要查找出父菜单下面全部的子菜单,然后根据子菜单的ID查找出该类别下面的全部新闻. 通常往往只知道父级菜单的ID,但不知道父级菜单下面有多少个子菜单,也不知道子菜单的ID 所以我们往往需要根 ...