Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 如何在 Activity 之間傳遞物件與物件陣列

編輯:關於 Android 程式設計

Activity 之間傳字串、傳基本型別很簡單,但要在頁與頁之間搬運自訂物件,就需要序列化的幫忙。以下用一個 People 類作為示範,裡面有姓名與年齡兩個欄位。

public class People {
    public String strName;
    public int iAge;

    public People(String strName, int iAge) {
        this.strName = strName;
        this.iAge = iAge;
    }

    public String getName() { return strName; }
    public int getAge() { return iAge; }
}

方法一:Serializable

前提很簡單,讓類別實作 Serializable 介面即可:

public class People implements Serializable {
    private static final long serialVersionUID = 1L;
    public String strName;
    public int iAge;

    public People(String strName, int iAge) {
        this.strName = strName;
        this.iAge = iAge;
    }

    public String getName() { return strName; }
    public int getAge() { return iAge; }
}

傳遞單一物件

發送端:

People people = new People("John", 21);
Intent intent = new Intent(SendActivity.this, RcvActivity.class);
Bundle bundle = new Bundle();
bundle.putSerializable("people", people);
intent.putExtras(bundle);
startActivity(intent);

接收端:

People people = (People) this.getIntent().getSerializableExtra("people");
String strData = people.getName() + " " + people.getAge();
Toast.makeText(getApplication(), strData, Toast.LENGTH_SHORT).show();

傳遞物件陣列

發送端:

List<People> people = new ArrayList<People>();
people.add(new People("John", 21));
people.add(new People("Amy", 20));

Bundle bundle = new Bundle();
bundle.putSerializable("people", (Serializable) people);
Intent intent = new Intent(SendActivity.this, RcvActivity.class);
intent.putExtras(bundle);
startActivity(intent);

接收端:

List<People> resultList = (List<People>) this.getIntent().getSerializableExtra("people");
String strData = "";
for (People p : resultList) {
    strData = strData + p.strName + " " + p.iAge + "\n";
}
Toast.makeText(getApplication(), strData, Toast.LENGTH_SHORT).show();

方法二:Parcelable

Parcelable 是 Android 專屬機制,效能明顯優於 Serializable,代價是要多寫一些樣板程式碼。

public class People implements Parcelable {
    public String strName;
    public int iAge;

    public People(String strName, int iAge) {
        this.strName = strName;
        this.iAge = iAge;
    }

    public String getName() { return strName; }
    public int getAge() { return iAge; }

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

    @Override
    public void writeToParcel(Parcel parcel, int arg1) {
        parcel.writeString(strName);
        parcel.writeInt(iAge);
    }

    public static final Parcelable.Creator<People> CREATOR = new Creator<People>() {
        public People createFromParcel(Parcel source) {
            People pTemp = new People("", 0);
            pTemp.strName = source.readString();
            pTemp.iAge = source.readInt();
            return pTemp;
        }

        public People[] newArray(int size) {
            return new People[size];
        }
    };
}

傳遞單一物件

發送端:

People people = new People("John", 21);
Intent intent = new Intent(SendActivity.this, RcvActivity.class);
Bundle bundle = new Bundle();
bundle.putParcelable("people", people);
intent.putExtras(bundle);
startActivity(intent);

接收端:

People people = (People) this.getIntent().getParcelableExtra("people");
String strData = people.getName() + " " + people.getAge();
Toast.makeText(getApplication(), strData, Toast.LENGTH_SHORT).show();

傳遞物件陣列

發送端:

List<People> peopleList = new ArrayList<People>();
peopleList.add(new People("John", 21));
peopleList.add(new People("Amy", 20));

Intent intent = new Intent(SendActivity.this, RcvActivity.class);
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("People", (ArrayList<? extends Parcelable>) peopleList);
intent.putExtras(bundle);
startActivity(intent);

接收端:

List<People> resultList = this.getIntent().getExtras().getParcelableArrayList("People");
String strData = "";
for (People p : resultList) {
    strData = strData + p.strName + " " + p.iAge + "\n";
}
Toast.makeText(getApplication(), strData, Toast.LENGTH_SHORT).show();

兩者該怎麼選

Serializable 只需 implements Serializable,等於幫物件打個標記,序列化由系統自動完成,寫起來輕鬆。

Parcelable 除了實作介面,還必須補上 describeContents()、writeToParcel(),以及一個靜態成員 CREATOR(需實作 Parcelable.Creator)。程式碼量明顯較多。

效能上的差異才是重點:Parcelable 在記憶體中的表現優於 Serializable,因此 Android 官方推薦使用 Parcelable。此外 Serializable 序列化時會產生大量臨時物件,容易造成頻繁的 GC。

但 Parcelable 並非萬用——它依賴這種序列化方式無法保證資料的持續性,所以當你需要把資料寫到磁碟保存時,還是應該選 Serializable。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved