Android中Parcelable接口用法
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接口用法的更多相关文章
- (转)Android中Parcelable接口用法
1. Parcelable接口 Interface for classes whose instances can be written to and restored from a Parcel. ...
- Android中Parcelable接口的使用
在做开发的过程中,序列化是非常常见的.比如要将对象保存本地磁盘或者在网络上传输等.实现序列化有两种方式,一种是实现Serializable接口,第二种是实现Parcelable. Serializab ...
- Android中Parcelable接口
1. Parcelable接口 Interface for classes whose instances can be written to and restored from a Parcel. ...
- Android中Parcelable与Serializable接口用法
转自: Android中Parcelable接口用法 1. Parcelable接口 Interface for classes whose instances can be written to a ...
- Android中的接口回调技术
Android中的接口回调技术有很多应用的场景,最常见的:Activity(人机交互的端口)的UI界面中定义了Button,点击该Button时,执行某个逻辑. 下面参见上述执行的模型,讲述James ...
- Android中回调接口的使用
MainActivity如下: package cn.testcallback; import android.os.Bundle; import android.app.Activity; /** ...
- android 中uri.parse()用法
android 中uri.parse()用法 1,调web浏览器 Uri myBlogUri = Uri.parse("http://xxxxx.com"); returnIt = ...
- Android中Parcelable和Serializable接口用法
1. Parcelable接口 Interface for classes whose instances can be written to and restored from a Parcel. ...
- Android开发中Parcelable接口的使用方法
在网上看到很多Android初入门的童鞋都在问Parcelable接口的使用方法,小编参考了相关Android教程,看到里面介绍的序列化方法主要有两种分别是实现Serializable接口和实现Par ...
随机推荐
- BFS HDOJ 2102 A计划
题目传送门 题意:中文题面 分析:双层BFS,之前写过类似的题.总结坑点: 1.步数小于等于T都是YES 2. 传送门的另一侧还是传送门或者墙都会死 3. 走到传送门也需要一步 #include &l ...
- HttpClient 使用
Api支持 HttpClient 是基于Task的异步方法组,支持取消.超时异步特性,其可以分类为以下: Restful: GetAsync,PostAsync,DeleteAsync,PutAsyn ...
- BZOJ2434 [Noi2011]阿狸的打字机(AC自动机 + fail树 + DFS序 + 线段树)
题目这么说的: 阿狸喜欢收藏各种稀奇古怪的东西,最近他淘到一台老式的打字机.打字机上只有28个按键,分别印有26个小写英文字母和'B'.'P'两个字母.经阿狸研究发现,这个打字机是这样工作的: 输入小 ...
- The 2015 China Collegiate Programming Contest H. Sudoku hdu 5547
Sudoku Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Subm ...
- iOS之05-三大特性之封装
本次主要学习面向对象的三大特性:封装.继承和多态中的封装 封装 1. 好处 降低耦合率 可重复调用类中的属性 提高安全性,外部不能随便修改变量的值,保证了数据的安全性 2. set方法 1.作用:提供 ...
- 【Cocos2d-x游戏开发】解决Cocos2d-x中文乱码的三种方法
众所周知,Cocos2d-x是一款不错的开源引擎,但是在Cocos2d-x中直接使用中文是无法正确显示的.比如下面的情况: 解决这个问题常用的有三种方法:1.通过转换为UTF-8编码来显示.2.使用i ...
- mysql的关于TABLE_SCHEMA的sql语句和nformation_schema表
1.查询sjcenter数据库里开头为sj_demo和sj_onlyinv的所有表的总条数 select sum(table_rows) from (select table_name,table_r ...
- vim g s 对比
vim g s http://blog.chinaunix.net/uid-10597892-id-3311441.html
- [转]基于gulp和webpack的前端工程化
本文样例代码 :https://github.com/demohi/learning-gulp 本文主要简单介绍一下基于gulp和webpack的前端工程化. 技术栈 React.js reFlux ...
- Android PhoneGap 利用 Activity 实现 CordovaInterface
1.修改main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns ...