Android接口Parcelable的使用
注明:非原创,转载,原链接地址为:http://www.2cto.com/kf/201205/132814.html 和 http://www.blogjava.net/lincode/archive/2011/09/16/358805.html
英文好的同学们可以直接看Android官网关于parcelable的介绍,链接:http://developer.android.com/reference/android/os/Parcelable.html
对于Android来说传递复杂类型,主要是将自己的类转换为基础的字节数组,Activity之间传递数据是通过Intent实现的。
Android序列化对象主要有两种方法,实现Serializable接口、或者实现Parcelable接口。实现Serializable接口是Java SE本身就支持的,而Parcelable是Android特有的功能,效率比实现Serializable接口高,而且还可以用在进程间通信(IPC)中。实现Serializable接口非常简单,声明一下就可以了。而实现Parcelable接口稍微复杂一些,但效率更高,推荐用这种方法提高性能。
一 序列化原因:
1.永久性保存对象,保存对象的字节序列到本地文件中;
2.通过序列化对象在网络中传递对象;
3.通过序列化在进程间传递对象。
二 至于选取哪种可参考下面的原则:
1.在使用内存的时候,Parcelable 类比Serializable性能高,所以推荐使用Parcelable类。
2.Serializable在序列化的时候会产生大量的临时变量,从而引起频繁的GC。
3.Parcelable不能使用在要将数据存储在磁盘上的情况,因为Parcelable不能很好的保证数据的持续性在外界有变化的情况下。尽管Serializable效率低点, 也不提倡用,但在这种情况下,还是建议你用Serializable 。
实现:
1 Serializable 的实现,只需要继承 implements Serializable 即可。这只是给对象打了一个标记,系统会自动将其序列化。
2 Parcelabel 的实现,需要在类中添加一个静态成员变量 CREATOR,这个变量需要继承 Parcelable.Creator 接口。
Parcelable接口的作用:实现了Parcelable接口的实例可以将自身的状态信息(状态信息通常指的是各成员变量的值)写入Parcel,也可以从Parcel中恢复其状态。 Parcel用来完成数据的序列化传递。
下面就介绍一下实现Parcelable接口的方法。 通过实现Parcelable接口序列化对象的步骤:
1、实现Parcelable接口。
2、并且实现Parcelable接口的public void writeToParcel(Parcel dest, int flags)方法 。
3、自定义类型中必须含有一个名称为CREATOR的静态成员,该成员对象要求实现Parcelable.Creator接口及其方法。简而言之:通过writeToParcel将你的对象映射成Parcel对象,再通过createFromParcel将Parcel对象映射成你的对象。
也可以将Parcel看成是一个流,通过writeToParcel把对象写到流里面,在通过createFromParcel从流里读取对象,只不过这个过程需要你来实现,因此写的顺序和读的顺序必须一致。示例代码: [java]
1: package com.yang.domain;
2:
3: import android.os.Parcel;
4: import android.os.Parcelable;
5:
6: public class Person implements Parcelable {
7: //这里定义了两个变量来说明读和写的顺序要一致
8: private Integer id; . private String name;
9:
10: public Person() {
11: }
12:
13: public Person(Integer id, String name) {
14:
15: this.id = id;
16: this.name = name;
17: }
18:
19: public Integer getId() {
20: return id;
21: }
22:
23: public void setId(Integer id) {
24: this.id = id;
25: }
26:
27: public String getName() {
28: return name;
29: }
30:
31: public void setName(String name) {
32: this.name = name;
33: }
34:
35: @Override
36: public int describeContents() {
37: return 0;
38: }
39:
40: @Override
41: public void writeToParcel(Parcel dest, int flags) {
42: // 把javanbean中的数据写到Parcel。先写id然后写name
43: dest.writeInt(this.id);
44: dest.writeString(this.name);
45: }
46:
47: // 添加一个静态成员,名为CREATOR,该对象实现了Parcelable.Creator接口
48: public static final Parcelable.Creator<Person> CREATOR = new Parcelable.Creator<Person>() {
49: @Override
50: public Person createFromParcel(Parcel source) {
51: // 从Parcel中读取数据,返回person对象
52: return new Person(source.readInt(), source.readString());
53: }
54:
55: @Override
56: public Person[] newArray(int size) {
57: return new Person[size];
58: }
59: };
60: }
注:Parcelable接口在Android当中有非常之多的子类,如下截图:

并且Intent当中也定义了很多关于Parcelable的get、set方法,如:
Intent putExtra(String name, Parcelable value) Add extended data to the intent. T getParcelableExtra(String name) Retrieve extended data from the intent. 并且Intent本身也实现了Parcelable接口,因此在Android开发当中是非常推荐以Parcelable作为工具传递复制对象。
Android接口Parcelable的使用的更多相关文章
- Android 的Parcelable接口
此文转载自http://www.cnblogs.com/renqingping/archive/2012/10/25/Parcelable.html 1. Parcelable接口 Interface ...
- Android中Parcelable接口
1. Parcelable接口 Interface for classes whose instances can be written to and restored from a Parcel. ...
- (转)Android中Parcelable接口用法
1. Parcelable接口 Interface for classes whose instances can be written to and restored from a Parcel. ...
- Android中Parcelable和Serializable接口用法
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 使用Parcelable序列化对象
转:http://ipjmc.iteye.com/blog/1314145 Android序列化对象主要有两种方法,实现Serializable接口.或者实现Parcelable接口.实现 ...
- android 开发Parcelable 怎么传值ArrayList
public class TradeEntity implements Parcelable{ public String id; //有关进度条的参数 ArrayList<TradeState ...
- Android中Parcelable的使用
转载请标明出处 :https://www.cnblogs.com/tangZH/p/10998065.html Parcelable与Serializable Serializable是Java为我 ...
- android基础---->Parcelable的使用
android中Parcelable序列化的使用,简单的记录一下. 目录导航: Serializable在android中的使用 Parcelable在android中的使用 Serializabl ...
随机推荐
- mysql for循环存储过程
DROP PROCEDURE IF EXISTS test_insert; DELIMITER ;; CREATE PROCEDURE test_insert () BEGIN DECLARE i i ...
- Spring 整合WebSocket, Error during WebSocket handshake: Unexpected response code: 302 还有200的错误
springboot 集成websocket 及其简单,,,但是管理端使用的是Spring,原生配置,发生这个错误,,,302 被重定向了...我起的是本地locallhost,把ip换成 local ...
- Kafka运维填坑(转)
前提: 只针对Kafka 0.9.0.1版本; 说是运维,其实偏重于问题解决; 大部分解决方案都是google而来, 我只是作了次搬运工; 有些问题的解决方案未必一定是通用的, 若应用到线上请慎重; ...
- Innodb引擎中Count(*)
select count(*)是MySQL中用于统计记录行数最常用的方法,count方法可以返回表内精确的行数. 在某些索引下是好事,但是如果表中有主键,count(*)的速度就会很慢,特别在千万记录 ...
- groovy 知识集锦
对应官方的<Program structure>的中文翻译 http://www.cnblogs.com/zhaoxia0815/p/7404387.html
- Spring.xml中配置注解context:annotation-config和context:component-scan简述
XML中context:annotation-config和context:component-scan简述 <context:annotation-config/> 中文意思:<上 ...
- Java学习笔记 -- Java定时调度工具Timer类
1 关于 (时间宝贵的小姐姐请跳过) 本教程是基于Java定时任务调度工具详解之Timer篇的学习笔记. 什么是定时任务调度 基于给定的时间点,给定的时间间隔或者给定的执行次数自动执行的任务. 在Ja ...
- vue项目tips
在main.js引入了封装好的各组件,包括echarts.自定义组件等
- 基于innodb_print_all_deadlocks从errorlog中解析MySQL死锁日志
本文是说明如何获取死锁日志记录的,不是说明如何解决死锁问题的. MySQL的死锁可以通过show engine innodb status;来查看,但是show engine innodb statu ...
- week07 codelab02 C72
ss 我们要改一下backendserver的service 因为要写几个api还要做很多操作 我们单独写出来 然后由service来调用 import json import os import p ...