Android中实现序列化有两种方式,一种是使用Serializable另一种就是Parcelable了。

Serializable本身就是JavaSE支持的,Parcelable是Android特有的,效率比Serializable高,据统计一般情况下能快10倍左右!但是Parcelable不能用在需要数据永久储存的情况,比如存到本地文件中,Parcelable不能保证在外界有变化的时候数据的持续性。本文分别对这两种方法结合示例进行讲解。

新建两个Activity,一个是MainActivity,一个是ReceiveObjectActivity,我们把Person对象从MainActivity中传送到ReceiveObjectActivity。

Serializable

Person
import java.io.Serializable;/** * Created by SparkYuan on 10/12/2015. */public class Person implements Serializable {    private static final long serialVersionUID = 1L;    private String name;    private int age;    public Person() {    }    public Person(String name, int age) {        this.name = name;        this.age = age;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }}

MainActivity中添加一个按钮 sendObjectBtn ,重写其 setOnClickListener 方法
 sendObjectBtn.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                Person person = new Person("Spark",23);                Intent intent = new Intent(getApplicationContext(),ReceiveObjectActivity.class);                Bundle bundle = new Bundle();                bundle.putSerializable("person",person);                intent.putExtra("bundle",bundle);                startActivity(intent);            }        });

ReceiveObjectActivity的onCreate()方法中
Intent intent = getIntent();Person person = (Person)intent.getBundleExtra("bundle").getSerializable("person");Log.d("Debug",person.getName());

运行结果


Parcelable

Person
import android.os.Parcel;import android.os.Parcelable;/** * Created by Daniel on 10/12/2015. */public class Person implements Parcelable {    private String name;    private int age;    public Person() {    }    public Person(String name, int age) {        this.name = name;        this.age = age;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }    @Override    public int describeContents() {        return 0;    }    @Override    public void writeToParcel(Parcel dest, int flags) {        dest.writeString(name);        dest.writeInt(age);    }    public static final Parcelable.Creator<Person> CREATOR = new Parcelable.Creator<Person>(){        @Override        //实现从Parcel容器中读取传递数据值,封装成Parcelable对象返回        public Person createFromParcel(Parcel source) {            return new Person(source.readString(),source.readInt());        }        @Override        //供外部类反序列化本类数组使用        public Person[] newArray(int size) {            return new Person[size];        }    };}

注: 实现Parcelable的过程有些麻烦,下面逐一讲解。 (1) public int describeContents()
内容描述,返回0就可以。 (2) public void writeToParcel(Parcel dest, int flags)
把Person对象转化成Parcel对象,dest是目标Parcel,flags标志object是如何被写的。0或者PARCELABLE_WRITE_RETURN_VALUE。 (3) public static final Parcelable.Creator<T> CREATOR createFromParcel将Parcel映射成Person对象(Parcelable对象)

在MainActivity中添加一个按钮 sendObjectBtn ,重写其 setOnClickListener 方法
sendObjectBtn.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                Person person = new Person("Spark",23);                Intent intent = new Intent(getApplicationContext(),ReceiveObjectActivity.class);                Bundle bundle = new Bundle();                bundle.putParcelable("person",person);//                bundle.putSerializable("person",person);                intent.putExtra("bundle",bundle);                startActivity(intent);            }        });

ReceiveObjectActivity的onCreate()方法中

Intent intent = getIntent();Person person = (Person)intent.getBundleExtra("bundle").getParcelable("person");Log.d("Debug",person.getName());

运行结果



更多相关文章

  1. Kivy A to Z -- Kivycatalog例子无法在Android平台上运行及异常
  2. Android(安卓)滑动手势侦测方法介绍
  3. Android工程中R.java文件的重新生成——注意资源文件的错误
  4. Android入门篇四:使用全局变量在Activity之间传递数据
  5. Android(安卓)EventBus框架入门
  6. Android和js进行交互
  7. 浅谈Java中Collections.sort对List排序的两种方法
  8. 类和 Json对象
  9. Python list sort方法的具体使用

随机推荐

  1. Android Error: Conversion to Dalvik fo
  2. Android知识储备
  3. Android的一些网上开发资源链接地址
  4. Android实践项目汇报(一)
  5. 第一章 JAVA入门(Android安全模型)
  6. Android 之 ScrollView(垂直滑动)组件
  7. Android(安卓)NDK开发(四) 将FFmpeg移植到A
  8. Android图片下载缓存库picasso解析
  9. Android使用ButterKnife与和风天气sdk
  10. Android权限设置大全