Android中如何使用Intent傳遞對象

來源:互聯網
上載者:User

標籤:android

我們可以藉助Intent來啟動活動、發送廣播、啟動服務等,在進行上述操作時,我們還能在Intent中添加一些附加資料,已達傳值的效果,比如下面的代碼:
MainActivity

Intent intent = new Intent(MainActivity.this, SecondActivity.class);intent.putExtra("name", "xiaoming");intent.putExtra("age", 17);startActivity(intent);

SecondActivity可以得到這些值:

getIntent().getStringExtra("name");getIntent().getIntExtra("age", 0);

這裡只是利用Intent傳遞一些值,如何傳遞對象呢?

有兩種方式,一種是Serializable方式,另一種是Parcelable方式。

Serializable是序列化的意思,表示將一個對象轉換成可儲存或者可傳輸的狀態。序列化後的對象可以在網路上進行傳輸,也可以儲存到本地。序列化方法很簡單,只需要讓一個類去實現Serializable這個介面就可以了。
比如一個Animal類,其中包含了type,sex這兩個欄位,代碼如下:

public class Animal implements Serializable{    private String type;    private String sex;    public String getType()    {        return type;    }    public void setType(String type)    {        this.type = type;    }    public Stirng getSex()    {        return sex;    }    public void setSex(String sex)    {        this.sex = sex;    }}

MainActivty.java

Animal animal = new Animal();animal.setType("panda");animal.setSex("male");Intent intent = new Intent(MainActivity.this, SecondActivity.class);intent,putExtra("animal_panda", animal);startActivity(intent);

SecondActivity.java

Animal animal = (Animal)getIntent().getSerializableExtra(animal_panda);

這裡調用了getSerializableExtra()方法擷取通過參數傳遞過來的序列化對象,接著再將它向下轉型成Person對下崗,這樣我們就成功實現了使用Intent來傳遞對象的功能了。

除了Serializable之外,使用Parcelable也可以實現相同的效果,不過不同於將對象進行序列化,Parcelable方式的實現原理是將一個完整的對象進行分解,而分解後的每一部分都是Intent所支援的資料類型,這樣就實現傳遞對象的功能了。

public class Animal implements Serializable{    private String type;    private String sex;    public String getType()    {        return type;    }    public void setType(String type)    {        this.type = type;    }    public Stirng getSex()    {        return sex;    }    public void setSex(String sex)    {        this.sex = sex;    }    @Override    public int describeContents()    {        return 0;    }    @Override    public void writeToParcel(Parcel dest, int flags)    {        dest.writeString(type);        dest.writeString(sex);    }    public static final Parcelable.Creator<Animal> CREATOR = new Parcelable.Creator<Animal>()    {        @Override        public Animal createFromParcel(Parcel source)        {            Animal animal = new Animal();            animal.type = source.readStirng();            animal.sex = source.readString();            return animal;        }        @Override         public Aniaml[] newArray(int size)        {            return new Animal[size];        }    }}

SecondActivity.java

Animal animal = (Animal)getIntent.getParcelableExtra(animal-panda);

存在即理由,Parcelable方式雖然複雜,但效率更高,一般推薦使用Parcelable方式來實現Intent傳遞的功能。

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

Android中如何使用Intent傳遞對象

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.