关于Intent传递对象以及序列化技巧

来源:互联网 发布:淘宝超市加盟 编辑:程序博客网 时间:2024/05/21 16:18

Intent能传递的数据类型有限,自定义的对象将无从下手。
第一个界面传

 Intent intent = new Intent(this,SecondActivity.class);        intent.putExtra("string_data","hello");        intent.putExtra("int_data",1);        startActivity(intent);

第二个界面取

Intent intent = getIntent();        String string_data = intent.getStringExtra("string_data");        int int_data = intent.getIntExtra("int_data",0);

关于方法的简略介绍

 /**     * Retrieve extended data from the intent.     *     * @param name The name of the desired item.     * @param defaultValue the value to be returned if no value of the desired     * type is stored with the given name.(没值,返回默认值)     *     * @return the value of an item that previously added with putExtra()     * or the default value if none was found.     *     * @see #putExtra(String, int)     */    public int getIntExtra(String name, int defaultValue) {        return mExtras == null ? defaultValue :            mExtras.getInt(name, defaultValue);    }

对于Intent可以看看方法实现的接口

public class Intent implements Parcelable, Cloneable {    private static final String ATTR_ACTION = "action";    private static final String TAG_CATEGORIES = "categories";    private static final String ATTR_CATEGORY = "category";    private static final String TAG_EXTRA = "extra";    private static final String ATTR_TYPE = "type";    private static final String ATTR_COMPONENT = "component";    private static final String ATTR_DATA = "data";    private static final String ATTR_FLAGS = "flags";

为什么要序列化?

序列化的原因基本三种情况:
1.永久性保存对象,保存对象的字节序列到本地文件中;
2.对象在网络中传递;
3.对象在IPC间传递。

序列化方法

在Android系统中关于序列化的方法一般有两种,分别是实现Serializable接口和Parcelable接口,其中Serializable接口是来自Java中的序列化接口,而Parcelable是Android自带的序列化接口。

上述的两种序列化接口都有各自不同的优缺点,我们在实际使用时需根据不同情况而定。

1.Serializable在序列化的时候会产生大量的临时变量,从而引起频繁的GC,而相比之下Parcelable的性能更高(毕竟是Android自带的),所以当在使用内存时(如:序列化对象在网络中传递对象或序列化在进程间传递对象),更推荐使用Parcelable接口。

Serializable的接口实现很简单,只需让需要序列化的类继承Serializable 即可,系统会自动将其序列化。

Serializable接口的实现及使用

public class Person implements Serializable {    private String name;    private int 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;    }}

第一个界面传

 Person person = new Person();        person.setName("xxx");        person.setAge(20);        Intent intent = new Intent(this,SecondActivity.class);        intent.putExtra("person_data",person);        startActivity(intent);

第二个界面取

 Person person =(Person) getIntent().getSerializableExtra("person_data");

这里是将Intent进行了向下转型成为Person对象

Parcelable接口的实现及使用

2.但Parcelable有个明显的缺点:不能使用在要将数据存储在磁盘上的情况(如:永久性保存对象,保存对象的字节序列到本地文件中),因为Parcel本质上为了更好的实现对象在IPC间传递,并不是一个通用的序列化机制,当改变任何Parcel中数据的底层实现都可能导致之前的数据不可读取,所以此时还是建议使用Serializable 。

实现Parcelable接口主要可以分为一下几步:
1)implements Parcelable。
2)重写writeToParcel方法,将你的对象序列化为一个Parcel对象,即:将类的数据写入外部提供的Parcel中,打包需要传递的数据到Parcel容器保存,以便从Parcel容器获取数据。
3)重写describeContents方法,内容接口描述,默认返回0即可。
4)实例化静态内部对象CREATOR实现接口Parcelable.Creator 。
注意:若将Parcel看成是一个流,则先通过writeToParcel把对象写到流里面,再通过createFromParcel从流里读取对象,因此类实现的写入顺序和读出顺序必须一致。

切记对于Parcelable顺序不能乱

person类

public class Person implements Parcelable {    private String name;    private int 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        public Person createFromParcel(Parcel source) {           Person person = new Person();            //读取姓名和年龄            person.name = source.readString();            person.age = source.readInt();            return person;        }        @Override        public Person[] newArray(int size) {            return  new Person[size];        }    };}

第一个界面

Person person = new Person();        person.setName("xxx");        person.setAge(20);        Intent intent = new Intent(this,SecondActivity.class);        intent.putExtra("person_data",person);        startActivity(intent);

第二个界面

 Person person =(Person) getIntent().getParcelableExtra("person_data");

传承者(Inheritors)欢迎各位纠正错误,评论,吐槽!!!

0 0