此文转载自http://www.cnblogs.com/renqingping/archive/2012/10/25/Parcelable.html

1. Parcelable接口

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。

2.实现Parcelable就是为了进行序列化,那么,为什么要序列化?

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

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

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

3.实现序列化的方法

Android中实现序列化有两个选择:一是实现Serializable接口(是JavaSE本身就支持的),一是实现Parcelable接口(是Android特有功能,效率比实现Serializable接口高效,可用于Intent数据传递,也可以用于进程间通信(IPC))。实现Serializable接口非常简单,声明一下就可以了,而实现Parcelable接口稍微复杂一些,但效率更高,推荐用这种方法提高性能。

注:Android中Intent传递对象有两种方法:一是Bundle.putSerializable(Key,Object),另一种是Bundle.putParcelable(Key,Object)。当然这些Object是有一定的条件的,前者是实现了Serializable接口,而后者是实现了Parcelable接口。

4.选择序列化方法的原则

1)在使用内存的时候,Parcelable比Serializable性能高,所以推荐使用Parcelable。

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

3)Parcelable不能使用在要将数据存储在磁盘上的情况,因为Parcelable不能很好的保证数据的持续性在外界有变化的情况下。尽管Serializable效率低点,但此时还是建议使用Serializable 。

5.应用场景

需要在多个部件(Activity或Service)之间通过Intent传递一些数据,简单类型(如:数字、字符串)的可以直接放入Intent。复杂类型必须实现Parcelable接口。

6Parcelable接口定义

  1. public interface Parcelable
  2. {
  3. //内容描述接口,基本不用管
  4. public int describeContents();
  5. //写入接口函数,打包
  6. public void writeToParcel(Parcel dest, int flags);
  7. //读取接口,目的是要从Parcel中构造一个实现了Parcelable的类的实例处理。因为实现类在这里还是不可知的,所以需要用到模板的方式,继承类名通过模板参数传入
  8. //为了能够实现模板参数的传入,这里定义Creator嵌入接口,内含两个接口函数分别返回单个和多个继承类实例
  9. public interface Creator<T>
  10. {
  11. public T createFromParcel(Parcel source);
  12. public T[] newArray(int size);
  13. }
  14. }

7、实现Parcelable步骤

1)implements Parcelable

2)重写writeToParcel方法,将你的对象序列化为一个Parcel对象,即:将类的数据写入外部提供的Parcel中,打包需要传递的数据到Parcel容器保存,以便从 Parcel容器获取数据

3)重写describeContents方法,内容接口描述,默认返回0就可以

4)实例化静态内部对象CREATOR实现接口Parcelable.Creator

  1. public static final Parcelable.Creator<T> CREATOR

注:其中public static final一个都不能少,内部对象CREATOR的名称也不能改变,必须全部大写。需重写本接口中的两个方法:createFromParcel(Parcel in) 实现从Parcel容器中读取传递数据值,封装成Parcelable对象返回逻辑层,newArray(int size) 创建一个类型为T,长度为size的数组,仅一句话即可(return new T[size]),供外部类反序列化本类数组使用。

简而言之:通过writeToParcel将你的对象映射成Parcel对象,再通过createFromParcel将Parcel对象映射成你的对象。也可以将Parcel看成是一个流,通过writeToParcel把对象写到流里面,在通过createFromParcel从流里读取对象,只不过这个过程需要你来实现,因此写的顺序和读的顺序必须一致。

代码如下:

  1. public class MyParcelable implements Parcelable
    {
  2. private int mData;
  3.  
  4. public int describeContents()
    {
  5. return 0;
  6. }
  7.  
  8. public void writeToParcel(Parcel out, int flags)
    {
  9. out.writeInt(mData);
  10. }
  11.  
  12. public static final Parcelable.Creator<MyParcelable> CREATOR = new Parcelable.Creator<MyParcelable>()
    {
  13. public MyParcelable createFromParcel(Parcel in)
    {
  14. return new MyParcelable(in);
  15. }
  16.  
  17. public MyParcelable[] newArray(int size)
    {
  18. return new MyParcelable[size];
  19. }
  20. };
  21.  
  22. private MyParcelable(Parcel in)
    {
  23. mData = in.readInt();
  24. }
  25. }

8Serializable实现与Parcelabel实现的区别

1)Serializable的实现,只需要implements  Serializable 即可。这只是给对象打了一个标记,系统会自动将其序列化。

2)Parcelabel的实现,不仅需要implements  Parcelabel,还需要在类中添加一个静态成员变量CREATOR,这个变量需要实现 Parcelable.Creator 接口。

两者代码比较:

1)创建Person类,实现Serializable

  1. public class Person implements Serializable
  2. {
  3. private static final long serialVersionUID = -7060210544600464481L;
  4. private String name;
  5. private int age;
  6.  
  7. public String getName()
  8. {
  9. return name;
  10. }
  11.  
  12. public void setName(String name)
  13. {
  14. this.name = name;
  15. }
  16.  
  17. public int getAge()
  18. {
  19. return age;
  20. }
  21.  
  22. public void setAge(int age)
  23. {
  24. this.age = age;
  25. }
  26. }

2)创建Book类,实现Parcelable

  1. public class Book implements Parcelable
  2. {
  3. private String bookName;
  4. private String author;
  5. private int publishDate;
  6.  
  7. public Book()
  8. {
  9.  
  10. }
  11.  
  12. public String getBookName()
  13. {
  14. return bookName;
  15. }
  16.  
  17. public void setBookName(String bookName)
  18. {
  19. this.bookName = bookName;
  20. }
  21.  
  22. public String getAuthor()
  23. {
  24. return author;
  25. }
  26.  
  27. public void setAuthor(String author)
  28. {
  29. this.author = author;
  30. }
  31.  
  32. public int getPublishDate()
  33. {
  34. return publishDate;
  35. }
  36.  
  37. public void setPublishDate(int publishDate)
  38. {
  39. this.publishDate = publishDate;
  40. }
  41.  
  42. @Override
  43. public int describeContents()
  44. {
  45. return 0;
  46. }
  47.  
  48. @Override
  49. public void writeToParcel(Parcel out, int flags)
  50. {
  51. out.writeString(bookName);
  52. out.writeString(author);
  53. out.writeInt(publishDate);
  54. }
  55.  
  56. public static final Parcelable.Creator<Book> CREATOR = new Creator<Book>()
  57. {
  58. @Override
  59. public Book[] newArray(int size)
  60. {
  61. return new Book[size];
  62. }
  63.  
  64. @Override
  65. public Book createFromParcel(Parcel in)
  66. {
  67. return new Book(in);
  68. }
  69. };
  70.  
  71. public Book(Parcel in)
  72. {
  73. bookName = in.readString();
  74. author = in.readString();
  75. publishDate = in.readInt();
  76. }
  77. }
  78.  

Android 的Parcelable接口的更多相关文章

  1. Android中Parcelable接口用法

    from: http://www.cnblogs.com/renqingping/archive/2012/10/25/Parcelable.html Interface for classes wh ...

  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接口用法

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

  5. Android中Parcelable与Serializable接口用法

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

  6. Android调用远程Service的参数和返回值都需要实现Parcelable接口

    import android.os.Parcel;import android.os.Parcelable; public class Person implements Parcelable{ pr ...

  7. Android Studio酷炫插件(一)——自动化快速实现Parcelable接口序列化

    https://blog.csdn.net/kroclin/article/details/40902721 一.前言相信数据序列化大家都多多少少有接触到,比如自定义了一个实体类,需要在activit ...

  8. Android中Parcelable和Serializable接口用法

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

  9. Android系统中Parcelable和Serializable的区别,自动化实现Parcelable接口的插件

    Parcelable和Serializable的区别 参考地址:http://greenrobot.me/devpost/android-parcelable-serializable/ 由于最终的区 ...

随机推荐

  1. Holographic Remoting Player

    https://developer.microsoft.com/en-us/windows/holographic/holographic_remoting_player http://www.cnb ...

  2. 解决 node-gyp command not found 的问题

    node-gyp明明已经安装了,但是不能执行,显示命令找不到,然后重装之,发现npm有一个提示信息: npm WARN prefer global node-gyp@3.4.0 should be i ...

  3. [LeetCode] First Unique Character in a String 字符串第一个不同字符

    Given a string, find the first non-repeating character in it and return it's index. If it doesn't ex ...

  4. vue.js 第二课

    实现响应的数据绑定和组合的视图组件. 让数据与Dom保持同步 . 在使用jq手工操作DOM时,我们的代码常常是命令式的.重复的.易错的.Vue.js拥抱数据驱动的视图概念.简单的讲,它意味着我们在普通 ...

  5. jQuery_mobile页面跳转事件学习

      <html>     <head>         <meta http-equiv="Content-Type" content="t ...

  6. Linux系统编程:基本I/O系统调用

    文件描述符 进程每打开一个文件的时候,会获得该文件的文件描述符,而后续的读写操作都把文件描述符作为参数.在用户空间或者内核空间,都是通过文件描述符来唯一地索引一个打开的文件.文件描述符使用int类型表 ...

  7. py-faster-rcnn之python引入_caffe.so

    本文并不给出"编写一个c++代码,然后编译为.so文件,然后在python中引入"的hello world,需要的请参考:http://www.oschina.net/questi ...

  8. MDK5 STM32编译问题汇总

    MDK5 STM32编译问题汇总 WIN8.KEIL-MDK-5 编译时,出现弹窗"The ARM C/C++ Compiler 已停止工作",关闭弹窗后,编译输出的窗口中出现如下 ...

  9. Day5-python基础之函数(二)

    生成器 迭代器 装饰器 模块   来个需求,一个列表中所有元素都+1 1.最容易想到的方法 for循环,找列表索引,对应每个值+1 list_old = [1,2,3,4,5,6,7,8,9] for ...

  10. BZOJ1492: [NOI2007]货币兑换Cash

    设$x_j$,$y_j$为第$j$天能买的A,B券数量,$f_i$为第$i$天的最大收益.$f_i=\max_{1\le j<i}a_ix_j+b_iy_j$,最大化$f_i$即找一个点$(x_ ...