android 如何使用Parcelable接口
今天,简单讲讲android如何使用Parcelable接口传递数据。
这个其实也很简单,之前我写过博客将关于Serializable接口传递对象,实现Parcelable接口也是为了传递对象,但效率更高,所以在网上查找了相关的资料,这里记录一下。
想要在两个activity之间传递对象,那么这个对象必须序列化,android中序列化一个对象有两种方式,一种是实现Serializable接口,这个非常简单,只需要声明一下就可以了,不痛不痒。但是android中还有一种特有的序列化方法,那就是实现Parcelable接口,使用这种方式来序列化的效率要高于实现Serializable接口。不过Serializable接口实在是太方便了,因此在某些情况下实现这个接口还是非常不错的选择。
1.实现序列化的方法
Android中实现序列化有两个选择:一是实现Serializable接口(是JavaSE本身就支持的),一是实现Parcelable接口(是Android特有功能,效率比实现Serializable接口高效,可用于Intent数据传递,也可以用于进程间通信(IPC))。实现Serializable接口非常简单,声明一下就可以了,而实现Parcelable接口稍微复杂一些,但效率更高,推荐用这种方法提高性能。
注:Android中Intent传递对象有两种方法:一是Bundle.putSerializable(Key,Object),另一种是Bundle.putParcelable(Key,Object)。当然这些Object是有一定的条件的,前者是实现了Serializable接口,而后者是实现了Parcelable接口。
2.选择序列化方法的原则
1)在使用内存的时候,Parcelable比Serializable性能高,所以推荐使用Parcelable。
2)Serializable在序列化的时候会产生大量的临时变量,从而引起频繁的GC。
3)Parcelable不能使用在要将数据存储在磁盘上的情况,因为Parcelable不能很好的保证数据的持续性在外界有变化的情况下。尽管Serializable效率低点,但此时还是建议使用Serializable 。
2.使用Parcelable的步骤
1.实现Parcelable接口
2.实现接口中的两个方法
public int describeContents();
public void writeToParcel(Parcel dest, int flags);
第一个方法是内容接口描述,默认返回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);}};
}
简单讲讲,其实实现Parcelable接口就是对对象序列化,使对象可以在Intent或进程之间传递。使用也很简单,首先写一个类,实现Parcelable接口,对每个变量设置get和set函数,然后重写public int describeContents();,这个固定返回0就可以。还需要重写public void writeToParcel(Parcel dest, int flags);这里面通过dest.writeString(username);将变量一个一个的序列化,然后在构造函数里写一个获取序列化数据的函数,参数为Parcel,比如public Person(Parcel source),通过source.readString()来一个一个的读取变量。这里需要注意一点,读取变量的顺序必须和之前写入变量的顺序保持一致,不然会出错。最后还需要实例化静态内部对象CREATOR实现接口Parcelable.Creator,实例化CREATOR时要实现其中的两个方法,其中createFromParcel
的功能就是从Parcel中读取我们的对象。public Person createFromParcel(Parcel source)和public Person[] newArray(int size)都是调用new Person(source)来读取序列化变量的,只是createFromParcel是读取一个对象,newArray是读取一个数组。
这里还需要注意一点,Parcelable只能对内存对象序列化,不能对需要存储在文件或SD等设备进行序列化,如果需要将对象序列化存储到文件,实现Serializable接口就可以了。
最后贴上Parcelable源码:
/** 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<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();* }* }</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接口就讲完了。
就这么简单。
android 如何使用Parcelable接口相关推荐
- Android中的Parcelable接口和Serializable用法和区别
Parcelable接口: Interface for classes whose instances can be written to and restored from a Parcel. Cl ...
- Android 快速实现Parcelable接口
Android序列化有两种方式, 实现Serializable, 这种直接 implements Serializable就好了. 另一种就是实现Parcelable 需要写一些代码. 好在有andr ...
- 包含c语言的序列化字符,Android Serializable与Parcelable原理与区别
一.序列化.反序列化是什么? (1) 名词解释 对象的序列化 : 把Java对象转换为字节序列并存储至一个储存媒介的过程. 对象的反序列化:把字节序列恢复为Java对象的过程. (2) 序列化详细解释 ...
- Android Serializable与Parcelable原理与区别
2019独角兽企业重金招聘Python工程师标准>>> 一.序列化.反序列化是什么? (1) 名词解释 对象的序列化 : 把Java对象转换为字节序列并存储至一个储存媒介的过程. 对 ...
- Android调用远程Service的参数和返回值都需要实现Parcelable接口
import android.os.Parcel; import android.os.Parcelable; public class Person implements Parcelable{ p ...
- Android中Parcelable接口用法
-- 通过writeToParcel将你的对象映射成Parcel对象,再通过createFromParcel将Parcel对象映射成你的对象.也可以将Parcel看成是一个流,通过writeToPa ...
- Android中的数据传递之Parcelable接口
对于Android来说传递复杂类型,主要是将自己的类转换为基础的字节数组,Activity之间传递数据是通过Intent实现的. Android序列化对象主要有两种方法,实现Serializable接 ...
- Android 的Parcelable接口
此文转载自http://www.cnblogs.com/renqingping/archive/2012/10/25/Parcelable.html 1. Parcelable接口 Interface ...
- Android --- Serializable 接口与 Parcelable 接口的使用方法和区别,怎么选择?
文章目录 一.前言: 二.Serializable 接口 三.Parcelable 接口 四.两种接口怎么选择 一.前言: 本节主要讲解 IPC 中的基本概念,Serializable 接口与 Par ...
最新文章
- windows安装redis数据库
- 【项目管理】认识项目相关方(干系人)管理
- mysql base64 乱码_PHP base64编码后解码乱码的解决办法
- 怎样在Linux系统中编译,Linux操作系统如何编译程序?
- 小技巧 ----- 位运算的一些结论与应用
- 【电脑百科】BT种子
- python---之np.prod() 函数计算数组元素乘积等
- 扫描识别工具Dynamic Web TWAIN使用教程:单独添加/删除对象
- 【DOS BAT】Windows下微信双开、微信多开
- jmeter快速修改协议、服务器名称或IP、端口号的方法
- 最新的紫猫编程学院从零开始学脚本值得学习吗
- phalcon 自动加载_Phalcon自动加载(PHP自动加载),phalcon加载php_PHP教程
- python连乘函数_python 连乘
- 如何为自己的 CSDN博客设置自定义域名?
- 2023年谷歌外链购买最全攻略
- python 如何绘制分叉图
- 我对计算机专业的看法及对未来的计划
- Potplayer制作视频缩略图的方法
- Arcgis中如何实现线转面
- (二)安装机器人操作系统-ros
热门文章
- Vim强制写入w!的分析
- 个人总结:性能测试常见问题案例与原因
- MySQL计划任务3(转)
- 插入ASP代码让网站数据库成为ASP木马
- Android中的Notification
- css display属性理解
- 使用Emit反射建立运行时实体模型
- Lis(bzoj 3532)
- spring中@param和mybatis中@param使用区别
- (转载)你好,C++(19)“老师,我这次四级考试过了没有?”——4.2 条件选择语句...