博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
序列化之Parcelable
阅读量:7242 次
发布时间:2019-06-29

本文共 2756 字,大约阅读时间需要 9 分钟。

  序列化主要是用来传递类的信息,一般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
{ /** * 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
CREATOR * = new Parcelable.Creator
() { * 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(); * }

 

转载于:https://www.cnblogs.com/likeshu/p/5131291.html

你可能感兴趣的文章
GYM 101522B. Bacteria Experiment
查看>>
剑指Offer - 平衡二叉树
查看>>
Python3编写网络爬虫07-基本解析库pyquery的使用
查看>>
用OpenSSL命令行生成证书文件
查看>>
多线程的使用
查看>>
html5音频视频专题
查看>>
html怎样可是使文本框内容不可修改
查看>>
Linux安装禅道项目管理软件
查看>>
Bootstrap的jq匿名函数,实现分页技术--博客园老牛大讲堂
查看>>
深入理解Aspnet Core之Identity(4)
查看>>
指针解析
查看>>
CentOs7安装部署Zabbix3.4
查看>>
计算机一些常见名词解释
查看>>
1162: 零起点学算法69——查找最大元素
查看>>
linux awk命令用法
查看>>
我和JSON那些事儿
查看>>
注册 创建 显示 刷新窗口(注建显新),事件,消息循环,消息处理,钩子,dll钩子. (注重理解其中的逻辑关系)...
查看>>
Linux强大屏幕截图方法,理论能截取任何图形界面,包括登录界面
查看>>
【分块】bzoj3226 [Sdoi2008]校门外的区间
查看>>
SpringSecurity (Spring权限验证)
查看>>