Android序列化之談?wù)剆erializable和parcelable

一、定義

  • 實(shí)現(xiàn)serializable是java原生的序列化的方式
  • parcelable是Android上特有的用于將對(duì)象序列化的一個(gè)接口

二、為什么要使用序列化呢

1、在我們平時(shí)的開(kāi)發(fā)中,少不了在組件間以及不同界面中傳遞數(shù)據(jù),對(duì)于復(fù)雜數(shù)據(jù)類(lèi)型,比如對(duì)象我們需要將其序列化來(lái)達(dá)到可存儲(chǔ)可傳輸?shù)哪康?/h3>

三、實(shí)踐兩種序列化方法傳遞對(duì)象

1、實(shí)現(xiàn)Serializable接口序列化對(duì)象

實(shí)現(xiàn)Serializable接口的Student類(lèi)

public class Student implements Serializable{

    private String name;
    private int age;

    public Student() {
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

使用intent傳遞對(duì)象

Student student = new Student();
student.setAge(18);
student.setName("張三");
intent.putExtra("student", student);

從intent中得到student對(duì)象

Intent intent = getIntent();
Student student = (Student) intent.getSerializableExtra("student");

2、實(shí)現(xiàn)parcelable接口序列化

實(shí)現(xiàn)Parcelable的student類(lèi)

public class Student implements Parcelable{

    private String name;
    private int age;

    public Student() {
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    /** ---------------------Parcelable need--------------------------- */
    protected Student(Parcel in) {
        age = in.readInt();
        name = in.readString();
    }

    public static final Creator<Student> CREATOR = new Creator<Student>() {
        @Override
        public Student createFromParcel(Parcel in) {
            return new Student(in);
        }

        @Override
        public Student[] newArray(int size) {
            return new Student[size];
        }
    };

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(age);
        dest.writeString(name);
    }
}

intent傳遞

Student student = new Student();
student.setAge(18);
student.setName("張三");
intent.putExtra("student", student);

在SecondActivity中

Intent intent = getIntent();
Student student = intent.getParcelableExtra("student");

四、總結(jié)

系統(tǒng)自動(dòng)幫我們做了需要的操作

  1. 主要是readXXX()方法讀?。?/li>
  2. writeXXX()方法寫(xiě)入;
  3. 兩種方式都可以將對(duì)象序列化后傳遞 但是在Android中建議使用 Parcelable對(duì)對(duì)象進(jìn)行序列化
    它的效率是serializable的十倍以上;因?yàn)閟erializable在序列化的過(guò)程中使用到了反射,這是很耗時(shí)的,并且會(huì)產(chǎn)生很多的臨時(shí)變量,這將導(dǎo)致頻繁GC;而parcelable實(shí)質(zhì)上是將對(duì)象分解進(jìn)行傳遞,分解后的每一部分都是intent所支持的普通數(shù)據(jù)類(lèi)型;
  4. 如果需要將對(duì)象進(jìn)行持久化存儲(chǔ),建議使用serializable,即使效率要低;因?yàn)閜arcelable在外界有變化的情況下不能保證數(shù)據(jù)的的持續(xù)性;
  5. 比起serializable,parcelable使用要麻煩些 代碼量會(huì)更多,但這并不影響我們使用它,畢竟效率杠杠的。
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容