1. 程式人生 > >FC 13.2 使用Intent傳遞物件(Serializable和Parcelable)

FC 13.2 使用Intent傳遞物件(Serializable和Parcelable)

使用Intent的putExtra方法可以傳遞資料,但是不能直接來傳遞自定義的物件。接下來學習兩種床底物件的方式

 

Serializable方式

Serializable是序列化的意思,表示將一個物件轉換成可儲存或可傳輸的狀態,序列化後可以再網路上傳輸,或儲存到本地。序列化的方法就是讓一個類實現Serializable介面(不需要重寫方法),然後這個類的所有物件就都是可序列化的了。

用法

  • 新建實體類person,實現Serializable介面。
  • 建立例項,直接傳入到putExtra方法裡。
  • 使用getParcelableExtra方法獲取傳遞過來的序列化物件。
public class Person implements Serializable{
    private String name;
    private int age;
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
    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;
    }
}
Person person = new Person("小明", 22);
Intent intentPerson = new Intent(MainActivity.this, SecondActivity.class);
intentPerson.putExtra("person", person);
startActivity(intentPerson);
Person person = (Person) getIntent().getSerializableExtra("person");
String personName = person.getName();
text1.setText(personName);

Parcelable方式

Parcelable也可以實現與Serializable相同的效果,不多不同於將物件進行序列化,Parcelable方式的實現原理是將一個完整的物件進行分解,分解後的每一部分都是Intent所支援的資料型別,繼而實現傳遞物件的功能。

用法

  • 建立people實體類,實現Parcelable介面
    • 重寫writeToParcel方法describeContents方法:describeContents直接返回0即可,describeContents方法需要呼叫Parcel的writeXXX方法將實體類people的屬性都寫出來。
    • 在實體類prople中提供名為CREATOR的常量,這裡建立Parcelable.Creator介面的一個實現,將泛型指定為people,重寫createFromParcel方法newArray方法,createFromParcel方法裡去讀取剛才寫出的欄位,建立people進行返回。這裡的讀取順序一定要按剛才寫入的順序完全相同才可以。newArray方法只需要將方法中的size傳入陣列即可。
  • 建立例項,直接傳入到putExtra方法裡。
  • 獲取物件是略有改動:使用getParcelableExtra方法。
public class People implements Parcelable {
    private String name;
    private int age;

    public People() {
    }
    public People(String name, int age) {
        this.name = name;
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public int getAge() {
        return age;
    }
    public void setName(String name) {
        this.name = name;
    }
    public void setAge(int age) {
        this.age = age;
    }
    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(name);
        dest.writeInt(age);
    }
    @Override
    public int describeContents() {
        return 0;
    }
    public static final Parcelable.Creator<People> CREATOR = new Parcelable.Creator<People>() {
        @Override
        public People createFromParcel(Parcel source) {
            People people = new People();
            people.name = source.readString();
            people.age = source.readInt();
            return people;
        }
        @Override
        public People[] newArray(int size) {
            return new People[size];
        }
    };
}
People people = new People("小明", 22);
Intent intentPeople = new Intent(MainActivity.this, SecondActivity.class);
intentPeople.putExtra("people", people);
startActivity(intentPeople);
People people = (People) getIntent().getParcelableExtra("people");
String peopleName = people.getName();
text2.setText(peopleName);

兩種方式的詳細講解

Parcelable的效能要強於Serializable.因此在絕大多數的情況下,Android還是推薦使用Parcelable來完成對類的序列化操作的。(但Parcelable也有不足的方面,因為Parcelable無法將資料進行持久化)。

這篇文章講的很好:文章