想要在两个activity之间传递对象,那么这个对象必须序列化,Android中序列化一个对象有两种方式,一种是实现Serializable接口,这个非常简单,只需要声明一下就可以了,不痛不痒。但是android中还有一种特有的序列化方法,那就是实现Parcelable接口,使用这种方式来序列化的效率要高于实现Serializable接口。不过Serializable接口实在是太方便了,因此在某些情况下实现这个接口还是非常不错的选择。 
使用Parcelable步骤: 
1.实现Parcelable接口 
2.实现接口中的两个方法

public int describeContents();
public void writeToParcel(Parcel dest, int flags);
  • 1
  • 2
  • 1
  • 2

第一个方法是内容接口描述,默认返回0就可以了 
第二个方法是将我们的对象序列化一个Parcel对象,也就是将我们的对象存入Parcel中 
3.实例化静态内部对象CREATOR实现接口Parcelable.Creator,实例化CREATOR时要实现其中的两个方法,其中createFromParcel的功能就是从Parcel中读取我们的对象。

也就是说我们先利用writeToParcel方法写入对象,再利用createFromParcel方法读取对象,因此这两个方法中的读写顺序必须一致,否则会出现数据紊乱,一会我会举例子。 
看一个代码示例:

public class Person implements Parcelable{private String username;private String nickname;private int age;public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getNickname() {return nickname;}public void setNickname(String nickname) {this.nickname = nickname;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public Person(String username, String nickname, int age) {super();this.username = username;this.nickname = nickname;this.age = age;}public Person() {super();}/*** 这里的读的顺序必须与writeToParcel(Parcel dest, int flags)方法中* 写的顺序一致,否则数据会有差错,比如你的读取顺序如果是:* nickname = source.readString();* username=source.readString();* age = source.readInt();* 即调换了username和nickname的读取顺序,那么你会发现你拿到的username是nickname的数据,* 而你拿到的nickname是username的数据* @param source*/public Person(Parcel source) {username = source.readString();nickname=source.readString();age = source.readInt();}/*** 这里默认返回0即可*/@Overridepublic int describeContents() {return 0;}/*** 把值写入Parcel中*/@Overridepublic void writeToParcel(Parcel dest, int flags) {dest.writeString(username);dest.writeString(nickname);dest.writeInt(age);}public static final Creator<Person> CREATOR = new Creator<Person>() {/*** 供外部类反序列化本类数组使用*/@Overridepublic Person[] newArray(int size) {return new Person[size];}/*** 从Parcel中读取数据*/@Overridepublic Person createFromParcel(Parcel source) {return new Person(source);}};
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84

最后贴上Parcelable源码,Google已经给了一个示例了:

/** Copyright (C) 2006 The Android Open Source Project** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at**      http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/package android.os;/*** Interface for classes whose instances can be written to* and restored from a {@link Parcel}.  Classes implementing the Parcelable* interface must also have a static field called <code>CREATOR</code>, which* is an object implementing the {@link Parcelable.Creator Parcelable.Creator}* interface.* * <p>A typical implementation of Parcelable is:</p>* * <pre>* 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&lt;MyParcelable&gt; CREATOR*             = new Parcelable.Creator&lt;MyParcelable&gt;() {*         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();*     }* }</pre>*/
public interface Parcelable {/*** Flag for use with {@link #writeToParcel}: the object being written* is a return value, that is the result of a function such as* "<code>Parcelable someFunction()</code>",* "<code>void someFunction(out Parcelable)</code>", or* "<code>void someFunction(inout Parcelable)</code>".  Some implementations* may want to release resources at this point.*/public static final int PARCELABLE_WRITE_RETURN_VALUE = 0x0001;/*** Bit masks for use with {@link #describeContents}: each bit represents a* kind of object considered to have potential special significance when* marshalled.*/public static final int CONTENTS_FILE_DESCRIPTOR = 0x0001;/*** 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.*/public int describeContents();/*** 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);/*** 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);}/*** Specialization of {@link Creator} that allows you to receive the* ClassLoader the object is being created in.*/public interface ClassLoaderCreator<T> extends 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()} and* using the given ClassLoader.** @param source The Parcel to read the object's data from.* @param loader The ClassLoader that this object is being created in.* @return Returns a new instance of the Parcelable class.*/public T createFromParcel(Parcel source, ClassLoader loader);}
}

android parcelable 详细介绍相关推荐

  1. Android:adb 详细介绍

    adb 详细介绍 2018年03月15日 15:34:54 yulle 阅读数:15228更多 个人分类: adb ADB,即 Android Debug Bridge,它是 Android 开发/测 ...

  2. Android Parcelable的介绍与使用

    Parcelable使用的一个地方就是在进程间传递一个自定义的较为复杂的对象,对Parcelable学习了一下写下自己的观点. 将一个对象比如说一个名字为Cartoon的自定义类,由一个activit ...

  3. Android ART详细介绍及配置参数详解

    配置 ART 本文介绍了如何配置 ART 及其编译选项.讨论的主题包括:系统映像预编译配置.dex2oat 编译选项,以及如何在 system 分区空间.data 分区空间和性能这三者之间取得平衡. ...

  4. 详细介绍Android中Parcelable的原理和使用方法

    今日推荐 经常阅读博客是个好习惯 推荐技术小黑屋的Blog 引言 本篇文章目的在于详细的理解Parcelable的使用,而不是死记代码 我的简书博客地址 (由于csdn的图片经常出现不显示问题,以后就 ...

  5. Android底层隐私数据,Android Intent传递数据底层分析详细介绍_Android_脚本之家

    Android  Intent传递数据底层分析详细介绍 我们知道在Activity切换时,如果需要向下一个ActivityB传递数据,可以借助Intent对象的putExtra方法. 但是不知各位有没 ...

  6. android中intent放数据类型,Android Intent传递数据底层分析详细介绍

    Android  Intent传递数据底层分析详细介绍 我们知道在Activity切换时,如果需要向下一个ActivityB传递数据,可以借助Intent对象的putExtra方法. 但是不知各位有没 ...

  7. Android --- AndroidManifest.xml文件内容详细介绍

    文章目录 1.android:label="@string/app_name" 2. android:icon="@mipmap/ic_launcher"与an ...

  8. Android多开和虚拟化--Docker概念的详细介绍

    本文只是对Docker的概念做了较为详细的介绍,并不涉及一些像Docker环境的安装以及Docker的一些常见操作和命令. 通过阅读本文你将知道以下概念: 容器 什么是Docker? Docker思想 ...

  9. Android SQLite 数据库详细介绍

    Android SQLite 数据库详细介绍我们在编写数据库应用软件时,需要考虑这样的问题:因为我们开发的软件可能会安装在很多用户的手机上,如果应用使用到了SQLite数据库,我们必须在用户初次使用软 ...

最新文章

  1. 6.深度学习练习:Initialization
  2. Java 中 Vector、ArrayList、List 使用深入剖析
  3. PCL学习笔记01:在Ubuntu上安装PCL
  4. HDU1233——还是通常工程(最小生成树,并查集)
  5. 盘式制动优于鼓式制动吗?
  6. HDMI之DDC通道
  7. 计算机dos全套教学视频,梦想之路DOS命令系列培训教程(视频打包)
  8. JAVA调用WebService的三种方法
  9. pcie转m2装系统win10_NVMe SSD安装Win10系统详解:小白秒懂
  10. 多元高斯分布(三)——高斯过程
  11. mysql is marked_快速解决MySQL:Table xxx is marked as crashed and should be repaired五个办法...
  12. Windows驱动的Checked (chk)和Free(fre)区别
  13. python智力问答游戏_Python语言编写智力问答小游戏功能
  14. 于数学极限定义的量词组合复杂度
  15. 为什么成立计算机维修社团,张家口煤矿机械制造高级技工学校学生计算机维修社团成立...
  16. java大作业开题报告_c++大作业选题报告.docx
  17. c语言交朋友问题大全,交朋友语言教案
  18. Linux grep -v 命令排除输出
  19. 0.45秒!以太坊平均网络传输时间又双叒叕缩短了;以太坊全球节点分布覆盖英国、法国及德国...
  20. Andorid实例,淘宝评分条,星级评分条应用

热门文章

  1. php怎么随机显示6个数,PHP里一个12成员的数组,随机挑出6/3/3个成员(不重复),然后重新赋值,有什么简洁的办法实现?...
  2. java图片缩放工具类,一个JAVA图形缩放处置工具类
  3. mysql 锁机制 mvcc_轻松理解MYSQL MVCC 实现机制
  4. linux shell 脚本 2,理解Linux Shell和基本的Shell脚本(2)
  5. opencv 显示图片(直接)
  6. C++基类和派生类的析构函数
  7. python 将txt 表格转化为excel
  8. vpwm的控制变频_变频V/F和矢量控制你知道区别吗?据说这四种控制没有几人能说清...
  9. ssr pac_阴阳师新SSR千姬什么时候上线 ssr千姬活动上线时间一览
  10. 文巾解题 12. 整数转罗马数字