序列化主要是用来传递类的信息,一般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的更多相关文章

  1. 安卓实现序列化之Parcelable接口

    安卓实现序列化之Parcelable接口 1.实现序列化的方法: Android中实现序列化有两个选择:一是实现Serializable接口(是JavaSE本身就支持的) .一是实现Parcelabl ...

  2. android 很详细的序列化过程Parcelable

    直接上代码:注释都写的很清楚了. public class Entry implements Parcelable{ public int userID; public String username ...

  3. Android为TV端助力 很详细的序列化过程Parcelable

    直接上代码:注释都写的很清楚了. public class Entry implements Parcelable{ public int userID; public String username ...

  4. Android序列化:Parcelable/Serializable

    1.易用性及速度 1.1 Serializable——简单易用 Serializable的作用是为了保存对象的属性到本地文件.数据库.网络流.rmi以方便数据传输,当然这种传输可以是程序内的也可以是两 ...

  5. android序列化(1)Parcelable与Serializable

    1.Android中实现序列化有两个选择 一是实现Serializable接口(是JavaSE本身就支持的),实现Serializable接口非常简单. 一是实现Parcelable接口(是Andro ...

  6. android序列化(2)Parcelable与Parcel

    1.简介 Parcel  : 包裹 Android采用这个它封装消息数据.这个是通过IBinder通信的消息的载体.需要明确的是Parcel用来存放数据的是内存(RAM),而不是永久性介质(Nand等 ...

  7. 序列化Serializable和Parcelable

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 简单记录下序列化Serializable和Parcelable的使用方法. Android中Intent如果要传递类对象,可以通过两 ...

  8. 【Android - IPC】之Serializable和Parcelable序列化

    1.序列化的目的 (1)永久的保存对象数据(将对象数据保存到文件或磁盘中): (2)通过序列化操作将对象数据在网络上进行传输(由于网络传输是以字节流的方式对数据进行传输的,因此序列化的目的是将对象数据 ...

  9. Parcelable和Serializable的区别

      一.Android为什么要序列化?什么是序列化,怎么进行序列化 why 为什么要了解序列化?—— 进行Android开发的时候,无法将对象的引用传给Activities或者Fragments,我们 ...

随机推荐

  1. POJ 2570

    思路:floyd + 位运算.map[i][j]的二进制位前26位表示i到j路径里面字母a-z的存在情况,为1说明该字母存在,为0不存在. #include<iostream> #incl ...

  2. CF_402F dp+组合数学

    题目链接:http://codeforces.com/problemset/problem/403/D /**算法分析: 这道题综合的考察了dp背包思想和组合数学 */ #include<bit ...

  3. Installing your app on your Windows RT device

    Alright… so my app is almost finished and I want to install it for real on my Surface tablet. How do ...

  4. HDU5418.Victor and World(状压DP)

    #include <iostream> #include <cstring> #include <cstdio> #include <cmath> #i ...

  5. PC问题-使用BAT方法设置IP地址

    ::------以下为批处理文件内容---- @echo off ::set slection1= set/p slection1=请输入IP地址: netsh interface ip set ad ...

  6. 七行jquery代码实现图片渐变切换【兼容ie6+、 Chrome、 Firefox】

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. error C2589: “(”: “::”右边的非法标记 error C2059: 语法错误 : “::

    1. 错误输出 ./zlibrary/ui/src/win32/w32widgets/W32VBorderBox.cpp(114) : error C2589: “(”: “::”右边的非法标记    ...

  8. ros和Android(一)

    ros和Android :first-child { margin-top: 0; } blockquote > :last-child { margin-bottom: 0; } img { ...

  9. hdu 1050 Moving Tables

    http://acm.hdu.edu.cn/showproblem.php?pid=1050 这个题我首先直接用的常规贪心,用的和那个尽可能看更多完整节目那种思路.但是.......一直WA....T ...

  10. Redis学习手册(Set数据类型)

    一.概述: 在Redis中,我们可以将Set类型看作为没有排序的字符集合,和List类型一样,我们也可以在该类型的数据值上执行添加.删除或判断某一元素是否存在等操作.需要说明的是,这些操作的时间复杂度 ...