from:

http://www.cnblogs.com/renqingping/archive/2012/10/25/Parcelable.html

Interface for classes whose instances can be written to and restored from a Parcel。 Classes implementing the Parcelable interface must also have a static field called CREATOR, which is an object implementing the Parcelable.Creator interface。

 public interface Parcelable {
public static final int PARCELABLE_WRITE_RETURN_VALUE = 0x0001;
public static final int CONTENTS_FILE_DESCRIPTOR = 0x0001;
public int describeContents();//返回0即可
    //一个是把本对象(实现了Parcelable的对象)的数据写入Parcel中
public void writeToParcel(Parcel dest, int flags);
  /**
* 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> {//提供一个内部类,供反序列化的时候把Parcel对象转化为对象,即本对象
/**
* 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);//读出source中的数据给T(即本对象) /**
* 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);
}
public interface ClassLoaderCreator<T> extends Creator<T> {
public T createFromParcel(Parcel source, ClassLoader loader);
}
}

2.序列化的原因

1)永久性保存对象,保存对象的字节序列到本地文件中;

2)通过序列化对象在网络中传递对象;

3)通过序列化在进程间传递对象。

3.Parcelable与Serializable的对比

  • Parcelable

  1.android特有;

  2.实现复杂;

  3.比Serializable高效

  4.可以用于进程间通信(IPC),也可用于intent通讯;

  5.不能使用在要将数据存储在磁盘上的情况,因为Parcelable不能很好的保证数据的持续性在外界有变化的情况下。

  6.复杂类型必须实现Parcelable接口。

  • Serializable

  1.java SE实现;

  2.在序列化的时候会产生大量的临时变量,从而引起频繁的GC。

  3.实现简单

Parcel是公共的内存空间,所以可以存、取数据。由于是内存操作,所以要比Serialize利用外部存储设备来存、取数据要快!

如果要发送对象最简单的方法是: 把要发的数据直接当做对象发送,接收也不用Bundle(这是可以的),因为已经默认创建。

4.public static final一个都不能少,内部对象CREATOR的名称也不能改变,必须全部大写。

简而言之:通过writeToParcel将你的对象映射成Parcel对象,再通过createFromParcel将Parcel对象映射成你的对象。

5.简单例子

 public class Person implements Parcelable {
private String name;
private int age;
private static final String TAG = "Person"; public String getName() {
return name;
} public void setName(final String name) {
this.name = name;
} public int getAge() {
return age;
} public void setAge(final int age) {
this.age = age;
} public static final Parcelable.Creator<Person> CREATOR = new Creator<Person>() {
@Override
public Person createFromParcel(final Parcel source) {
Log.d(TAG, "createFromParcel");
Person mPerson = new Person();
mPerson.name = source.readString();
mPerson.age = source.readInt();
return mPerson;
} @Override
public Person[] newArray(final int size) {
// TODO Auto-generated method stub
return new Person[size];
}
}; @Override
public int describeContents() {
// TODO Auto-generated method stub
Log.d(TAG, "describeContents");
return 0;
} @Override
public void writeToParcel(final Parcel dest, final int flags) {
// TODO Auto-generated method stub
Log.d(TAG, "writeToParcel");
dest.writeString(name);
dest.writeInt(age);
}
}

6.writeToParcel()写和createFromParcel()读对象的数据们的顺序必须一致;



Android中Parcelable接口用法的更多相关文章

  1. (转)Android中Parcelable接口用法

    1. Parcelable接口 Interface for classes whose instances can be written to and restored from a Parcel. ...

  2. Android中Parcelable接口的使用

    在做开发的过程中,序列化是非常常见的.比如要将对象保存本地磁盘或者在网络上传输等.实现序列化有两种方式,一种是实现Serializable接口,第二种是实现Parcelable. Serializab ...

  3. Android中Parcelable接口

    1. Parcelable接口 Interface for classes whose instances can be written to and restored from a Parcel. ...

  4. Android中Parcelable与Serializable接口用法

    转自: Android中Parcelable接口用法 1. Parcelable接口 Interface for classes whose instances can be written to a ...

  5. Android中的接口回调技术

    Android中的接口回调技术有很多应用的场景,最常见的:Activity(人机交互的端口)的UI界面中定义了Button,点击该Button时,执行某个逻辑. 下面参见上述执行的模型,讲述James ...

  6. Android中回调接口的使用

    MainActivity如下: package cn.testcallback; import android.os.Bundle; import android.app.Activity; /** ...

  7. android 中uri.parse()用法

    android 中uri.parse()用法 1,调web浏览器 Uri myBlogUri = Uri.parse("http://xxxxx.com"); returnIt = ...

  8. Android中Parcelable和Serializable接口用法

    1. Parcelable接口 Interface for classes whose instances can be written to and restored from a Parcel. ...

  9. Android开发中Parcelable接口的使用方法

    在网上看到很多Android初入门的童鞋都在问Parcelable接口的使用方法,小编参考了相关Android教程,看到里面介绍的序列化方法主要有两种分别是实现Serializable接口和实现Par ...

随机推荐

  1. js公有、私有、静态属性和方法的区别

          现下,javascript大行其道,对于网站开发人员来说,javascript是必需掌据的一门语言,但随着jquery等框架的流行和使用,许多人对于原生javascript缺乏深入的理解, ...

  2. 每天一个Linux命令---tcpdump

    用简单的话来定义tcpdump,就是:dump the traffic on a network,根据使用者的定义对网络上的数据包进行截获的包分析工具. tcpdump可以将网络中传送的数据包的“头” ...

  3. TODO软件工程--如何预算项目的工期

    我的项目后台接口已经开发好,是前人留下的接口,现在只需要和前端联调,本以为后台的开发周期短,可以提早上线,可以因为旧接口不兼容新的要求 不得不重新写,造成了工期的延误. 如何计算一个工期被提上日程.

  4. C++可能出错的小细节

    1. for(list<Geometry_line>::iterator it = G.begin(); it != G.end();) { if(IsLineCrossed(*it, l ...

  5. ccc let

    let,其实就是块级作用域申明变量的var.之前JS的var关键字是非块级作用域的,而是函数级的. 例如arr=[0,1,2],我们经常写循环 for(var i=0,len=arr.length; ...

  6. java中的IO操作总结

    一.InputStream重用技巧(利用ByteArrayOutputStream) 对同一个InputStream对象进行使用多次. 比如,客户端从服务器获取数据 ,利用HttpURLConnect ...

  7. HDU 2822 (BFS+优先队列)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2822 题目大意:X消耗0,.消耗1, 求起点到终点最短消耗 解题思路: 每层BFS的结点,优先级不同 ...

  8. Codeforces Round #235 (Div. 2) A. Vanya and Cards

    #include <iostream> using namespace std; int main(){ int n,x; cin >> n >> x; ; ; i ...

  9. iOS 让启动页面延迟的方法

    <1> 利用多线程的方法:[NSThread sleepForTimeInterval:300];

  10. centos 7 搭建本地yum仓库

    首先需要创建一个目录 mkdir /1   #在根目录下创建一个名字为1的目录 将光盘挂载到创建的这个目录 mount /dev/cdrom /1 yum命令配置文件在/etc/yum.repos.d ...