Android中Parcelable接口用法

1. Parcelable接口数组


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。网络


2.实现Parcelable就是为了进行序列化,那么,为何要序列化?ide


1)永久性保存对象,保存对象的字节序列到本地文件中;函数


2)经过序列化对象在网络中传递对象;性能


3)经过序列化在进程间传递对象。this


3.实现序列化的方法spa


Android中实现序列化有两个选择:一是实现Serializable接口(是JavaSE自己就支持的),一是实现Parcelable接口(是Android特有功能,效率比实现Serializable接口高效,可用于Intent数据传递,也能够用于进程间通讯(IPC))。实现Serializable接口很是简单,声明一下就能够了,而实现Parcelable接口稍微复杂一些,但效率更高,推荐用这种方法提升性能。rest


注:Android中Intent传递对象有两种方法:一是Bundle.putSerializable(Key,Object),另外一种是Bundle.putParcelable(Key,Object)。固然这些Object是有必定的条件的,前者是实现了Serializable接口,然后者是实现了Parcelable接口。code


4.选择序列化方法的原则orm


1)在使用内存的时候,Parcelable比Serializable性能高,因此推荐使用Parcelable。


2)Serializable在序列化的时候会产生大量的临时变量,从而引发频繁的GC。


3)Parcelable不能使用在要将数据存储在磁盘上的状况,由于Parcelable不能很好的保证数据的持续性在外界有变化的状况下。尽管Serializable效率低点,但此时仍是建议使用Serializable 。


5.应用场景


须要在多个部件(Activity或Service)之间经过Intent传递一些数据,简单类型(如:数字、字符串)的能够直接放入Intent。复杂类型必须实现Parcelable接口。


六、Parcelable接口定义


public interface Parcelable 
{    //内容描述接口,基本不用管
    public int describeContents();    
    //写入接口函数,打包
    public void writeToParcel(Parcel dest, int flags);    
    //读取接口,目的是要从Parcel中构造一个实现了Parcelable的类的实例处理。由于实现类在这里仍是不可知的,因此须要用到模板的方式,继承类名经过模板参数传入    
    //为了可以实现模板参数的传入,这里定义Creator嵌入接口,内含两个接口函数分别返回单个和多个继承类实例    
    public interface Creator<T> 
    {           
         public T createFromParcel(Parcel source);           
         public T[] newArray(int size);
    }
}

复制代码

七、实现Parcelable步骤


1)implements Parcelable


2)重写writeToParcel方法,将你的对象序列化为一个Parcel对象,即:将类的数据写入外部提供的Parcel中,打包须要传递的数据到Parcel容器保存,以便从 Parcel容器获取数据


3)重写describeContents方法,内容接口描述,默认返回0就能够


4)实例化静态内部对象CREATOR实现接口Parcelable.Creator

public static final Parcelable.Creator<T> CREATOR

注:其中public static final一个都不能少,内部对象CREATOR的名称也不能改变,必须所有大写。需重写本接口中的两个方法:createFromParcel(Parcel in) 实现从Parcel容器中读取传递数据值,封装成Parcelable对象返回逻辑层,newArray(int size) 建立一个类型为T,长度为size的数组,仅一句话便可(return new T[size]),供外部类反序列化本类数组使用。


简而言之:经过writeToParcel将你的对象映射成Parcel对象,再经过createFromParcel将Parcel对象映射成你的对象。也能够将Parcel当作是一个流,经过writeToParcel把对象写到流里面,在经过createFromParcel从流里读取对象,只不过这个过程须要你来实现,所以写的顺序和读的顺序必须一致。


代码以下:


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();     } }

复制代码

八、Serializable实现与Parcelabel实现的区别


1)Serializable的实现,只须要implements  Serializable 便可。这只是给对象打了一个标记,系统会自动将其序列化。


2)Parcelabel的实现,不只须要implements  Parcelabel,还须要在类中添加一个静态成员变量CREATOR,这个变量须要实现 Parcelable.Creator 接口。


二者代码比较:


1)建立Person类,实现Serializable


public class Person implements Serializable
{    private static final long serialVersionUID = -7060210544600464481L;    
     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;
    }
}

复制代码

2)建立Book类,实现Parcelable


public class Book implements Parcelable
{    private String bookName;    
     private String author;    
     private int publishDate;    
     
    public Book()
    {
        
    }    
    public String getBookName()
    {        return bookName;
    }    
    public void setBookName(String bookName)
    {        this.bookName = bookName;
    }    
    public String getAuthor()
    {        return author;
    }    
    public void setAuthor(String author)
    {        this.author = author;
    }    
    public int getPublishDate()
    {        return publishDate;
    }    
    public void setPublishDate(int publishDate)
    {        this.publishDate = publishDate;
    }
    
    @Override    public int describeContents()
    {        return 0;
    }
    
    @Override    
    public void writeToParcel(Parcel out, int flags)
    {
        out.writeString(bookName);
        out.writeString(author);
        out.writeInt(publishDate);
    }    
    public static final Parcelable.Creator<Book> CREATOR = new Creator<Book>()
    {
        @Override        
        public Book[] newArray(int size)
        {            return new Book[size];
        }
        
        @Override        
        public Book createFromParcel(Parcel in)
        {            return new Book(in);
        }
    };    
    public Book(Parcel in)
    {
        bookName = in.readString();
        author = in.readString();
        publishDate = in.readInt();
    }
}
相关文章
相关标签/搜索