- Intent 的 putExtra() 方法中所支持的數(shù)據(jù)類型是有限的划址,雖然常用的一些數(shù)據(jù)類型它都會支持支子,但是當(dāng)想傳遞一些自定義對象的時候就會發(fā)現(xiàn)無從下手带膀。這時需要使用** Serializable 方式或 Parcelable 方式**來傳遞對象箱靴。
一淳地、 Serializable 方式
- Serializable 是序列化的意思捡偏,表示將一個對象轉(zhuǎn)換成可存儲或可傳輸?shù)臓顟B(tài)唤冈。
1. 要使一個類的對象可序列化,只需要讓這個類去實(shí)現(xiàn) Serializable 這個接口就可以了银伟。
比如說有一個 Person 類你虹,其中包含了 name 和 age 這兩個字段绘搞,想要將它序列化就可以這樣寫,這樣所有的 Person 對象就都是可序列化的了:
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;
}
}
2. 傳遞序列化的對象傅物。
Person person = new Person();
person.setName("Tom");
person.setAge(20);
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("person_data", person);
startActivity(intent);
- 由于 Person 類實(shí)現(xiàn)了 Serializable 接口夯辖,所以才可以這樣寫。
3. 獲取序列化的對象董饰。
Person person = (Person) getIntent().getSerializableExtra("person_data");
- 調(diào)用 getSerializableExtra() 方法來獲取通過參數(shù)傳遞過來的序列化對象蒿褂,接著再將它向下轉(zhuǎn)型成 Person 對象。
二卒暂、Parcelable 方式
- Parcelable 方式的實(shí)現(xiàn)原理是將一個完整的對象進(jìn)行分解啄栓,而分解后的每一部分都是 Intent 所支持的數(shù)據(jù)類型,這樣也就實(shí)現(xiàn)傳遞對象的功能了也祠。
1. Parcelable 的實(shí)現(xiàn)方式昙楚。
public class Person implements Parcelable {
private String name;
private int age;
......
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name); // 寫出name
dest.writeInt(age); // 寫出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(); // 讀取name
person.age = source.readInt(); // 讀取age
return person;
}
@Override
public Person[] newArray(int size) {
return new Person[size];
}
};
}
- 首先讓 Person 類去實(shí)現(xiàn)了** Parcelable 接口**,這樣就必須重寫 **describeContents() 和 writeToParcel() **這兩個方法诈嘿。
- describeContents() 方法直接返回 0 就可以了.
- ** writeToParcel()** 方法中我們需要調(diào)用 Parcel 的** writeXxx() 方法將 Person 類中的字段一一寫出堪旧。注意字符串型數(shù)據(jù)**就調(diào)用 writeString() 方法,整型數(shù)據(jù)就調(diào)用 **writeInt() **方法永淌,以此類推崎场。
- 還必須在 Person 類中提供一個名為 **CREATOR **的常量,這里創(chuàng)建了 Parcelable.Creator 接口的一個實(shí)現(xiàn)遂蛀,并將泛型指定為 Person谭跨。接著需要重寫 **createFromParcel() **和 newArray() 這兩個方法。
- 在 **createFromParcel() **方法中要去讀取剛才寫出的 name 和 age 字段李滴,并創(chuàng)建一個 Person 對象進(jìn)行返回螃宙,其中 name 和 age 都是調(diào)用 Parcel 的 readXxx() 方法讀取到的,注意這里讀取的順序一定要和剛才寫出的順序完全相同所坯。
- **newArray() **方法中的實(shí)現(xiàn)就簡單多了谆扎,只需要 new 出一個 Person 數(shù)組,并使用方法中傳入的 size 作為數(shù)組大小就可以了芹助。
2. 傳遞對象堂湖。
Person person = new Person();
person.setName("Tom");
person.setAge(20);
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("person_data", person);
startActivity(intent);
- 傳遞對象方法與 Serializable 方式一樣。
3. 獲取對象状土。
Person person = (Person) getIntent().getParcelableExtra("person_data");
- 調(diào)用 getParcelableExtra() 方法來獲取對象无蜂,接著再將它向下轉(zhuǎn)型成 Person 對象。
3. 對比
Serializable 方式較為簡單蒙谓,但由于會把整個對象進(jìn)行序列化斥季,因此效率較低。所以在通常情況下還是更加推薦使用 Parcelable 方式來實(shí)現(xiàn) Intent 傳遞對象的功能累驮。