Android編程使用Intent傳遞對象的方法分析
本文實(shí)例分析了Android編程使用Intent傳遞對象的方法。分享給大家供大家參考,具體如下:
之前的文章中,介紹過Intent的用法,比如啟動活動,發(fā)送廣播,啟發(fā)服務(wù)等,并且可以使用Intent時(shí)傳遞一些數(shù)據(jù)。如下代碼所示:
Intent intent = new Intent(this,SecondActivity.class); intent.putExtra("info", "I am fine"); startActivity(intent);
在傳遞數(shù)據(jù)時(shí),使用的方法是putExtra,支持的數(shù)據(jù)類型有限,如何傳遞對象呢??
在Android中,使用Intent傳遞對象有兩種方式:Serializable序列化方式以及Parcelable串行化方式。
1、Serializable方式
此種方式表示將一個對象轉(zhuǎn)換成可存儲或者可傳輸?shù)臓顟B(tài),序列化后的對象可以在網(wǎng)絡(luò)上進(jìn)行傳輸,可以存儲到本地。
對象序列化,只需要實(shí)現(xiàn)Serializable類。
package com.example.testapplication; import java.io.Serializable; /** * 對象序列化 * @author yy * */ public class Emp 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; } }
那么Intent如何傳遞對象參數(shù)呢,查看API發(fā)現(xiàn)如下方法:
因此,使用該方法傳遞,如下:
Intent intent = new Intent(this,SecondActivity.class); intent.putExtra("obj", new Emp()); startActivity(intent);
那么如何獲取呢?使用如下方法:
這樣就獲得了Emp對象了。
2、Parcelable方式
該種方式的實(shí)現(xiàn)原理是將一個完整的對象進(jìn)行分解,使分解的每一部分都是Intent所支持的數(shù)據(jù)類型。示例如下:
package com.example.testapplication; import android.os.Parcel; import android.os.Parcelable; /** * Parcelable方式 * @author yy * */ public class Emp2 implements Parcelable{ 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; } @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flag) { //寫出name dest.writeString(name); //寫出age dest.writeInt(age); } public static final Parcelable.Creator<Emp2> creator = new Creator<Emp2>() { @Override public Emp2[] newArray(int size) { return new Emp2[size]; } @Override public Emp2 createFromParcel(Parcel source) { Emp2 emp2 = new Emp2(); //讀取的順序要和上面寫出的順序一致 //讀取name emp2.name = source.readString(); emp2.age = source.readInt(); return emp2; } }; }
傳遞對象:方式和序列化相同:
Intent intent = new Intent(this,SecondActivity.class); intent.putExtra("obj", new Emp2()); startActivity(intent);
獲取對象:
3、區(qū)別
Serializable在序列化的時(shí)候會產(chǎn)生大量的臨時(shí)變量,從而引起頻繁的GC。因此,在使用內(nèi)存的時(shí)候,Parcelable 類比Serializable性能高,所以推薦使用Parcelable類。
Parcelable不能使用在要將數(shù)據(jù)存儲在磁盤上的情況,因?yàn)镻arcelable不能很好的保證數(shù)據(jù)的持續(xù)性在外界有變化的情況下。盡管Serializable效率低點(diǎn), 也不提倡用,但在這種情況下,還是建議你用Serializable 。
希望本文所述對大家Android程序設(shè)計(jì)有所幫助。
- Android 通過Intent使用Bundle傳遞對象詳細(xì)介紹
- 在Android中通過Intent使用Bundle傳遞對象的使用方法
- Android中Intent傳遞對象的3種方式詳解
- 詳解Android中Intent傳遞對象給Activity的方法
- Android開發(fā)中Intent傳遞對象的方法分析
- Android中Intent傳遞對象的兩種方法Serializable,Parcelable
- Android中使用Intent在Activity之間傳遞對象(使用Serializable或者Parcelable)的方法
- Android系列之Intent傳遞對象的幾種實(shí)例方法
- Android Intent傳遞對象的兩種方法(Serializable,Parcelable)詳細(xì)介紹
相關(guān)文章
Android設(shè)置鈴聲實(shí)現(xiàn)代碼
這篇文章主要介紹了Android設(shè)置鈴聲實(shí)現(xiàn)代碼,以實(shí)例形式分析了Android中鈴聲設(shè)置的相關(guān)技巧,非常簡單實(shí)用,需要的朋友可以參考下2015-10-10android 更改TextView中任意位置字體大小和顏色的方法
下面小編就為大家分享一篇android 更改TextView中任意位置字體大小和顏色的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01Android自定義SeekBar實(shí)現(xiàn)滑動驗(yàn)證且不可點(diǎn)擊
這篇文章主要為大家詳細(xì)介紹了Android自定義SeekBar實(shí)現(xiàn)滑動驗(yàn)證且不可點(diǎn)擊,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-03-03Android在一個app中安裝并卸載另一個app的示例代碼
這篇文章主要介紹了Android在一個app中安裝并卸載另一個app的示例代碼,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03Android RefreshLayout實(shí)現(xiàn)下拉刷新布局
這篇文章主要為大家詳細(xì)介紹了Android RefreshLayout實(shí)現(xiàn)下拉刷新布局,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10Android沉浸式狀態(tài)欄微技巧(帶你真正理解沉浸式模式)
因?yàn)锳ndroid官方從來沒有給出過沉浸式狀態(tài)欄這樣的命名,只有沉浸式模式(Immersive Mode)這種說法.下面通過本文給大家介紹Android沉浸式狀態(tài)欄微技巧,需要的朋友參考下2016-12-12Android 中Volley二次封裝并實(shí)現(xiàn)網(wǎng)絡(luò)請求緩存
這篇文章主要介紹了Android 中Volley二次封裝并實(shí)現(xiàn)網(wǎng)絡(luò)請求緩存的相關(guān)資料,希望通過本文能幫助到大家,徹底會使用Volley,需要的朋友可以參考下2017-09-09