Android中傳遞對(duì)象的三種方法的實(shí)現(xiàn)
Android中,Activity和Fragment之間傳遞對(duì)象,可以通過(guò)將對(duì)象序列化并存入Bundle或者Intent中進(jìn)行傳遞,也可以將對(duì)象轉(zhuǎn)化為JSON字符串,進(jìn)行傳遞。
序列化對(duì)象可以使用Java的Serializable的接口、Parcelable接口。轉(zhuǎn)化成JSON字符串,可以使用Gson等庫(kù)。
1.Serializable
public class Author implements Serializable{ private int id; private String name; //... }
public class Book implements Serializable{ private String title; private Author author; //... }
傳遞數(shù)據(jù)
Book book=new Book(); book.setTitle("Java編程思想"); Author author=new Author(); author.setId(1); author.setName("Bruce Eckel"); book.setAuthor(author); Intent intent=new Intent(this,SecondActivity.class); intent.putExtra("book",book); startActivity(intent);
接收數(shù)據(jù)
Book book= (Book) getIntent().getSerializableExtra("book"); Log.d(TAG,"book title->"+book.getTitle()); Log.d(TAG,"book author name->"+book.getAuthor().getName());
2.轉(zhuǎn)化為JSON字符串
public class Author{ private int id; private String name; //... }
public class Book{ private String title; private Author author; //... }
傳遞數(shù)據(jù)
Book book=new Book(); book.setTitle("Java編程思想"); Author author=new Author(); author.setId(1); author.setName("Bruce Eckel"); book.setAuthor(author); Intent intent=new Intent(this,SecondActivity.class); intent.putExtra("book",new Gson().toJson(book)); startActivity(intent);
接收數(shù)據(jù)
String bookJson=getIntent().getStringExtra("book"); Book book=new Gson().fromJson(bookJson,Book.class); Log.d(TAG,"book title->"+book.getTitle()); Log.d(TAG,"book author name->"+book.getAuthor().getName());
3.使用Parcelable
實(shí)現(xiàn)Parcelable接口需要實(shí)現(xiàn)兩個(gè)方法
- describeContents方法。內(nèi)容接口描述,默認(rèn)返回0就可以;
- writeToParcel方法。將傳遞的數(shù)據(jù)打包到Parcel容器中。
除了要實(shí)現(xiàn)這兩個(gè)方法還必須創(chuàng)建一個(gè)Parcelable.Creator接口的實(shí)例,用于讀取Parcel容器中的數(shù)據(jù)
public class Author implements Parcelable{ private int id; private String name; //setter & getter... @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags) { //該方法將類(lèi)的數(shù)據(jù)寫(xiě)入外部提供的Parcel中.即打包需要傳遞的數(shù)據(jù)到Parcel容器保存, // 以便從parcel容器獲取數(shù)據(jù) dest.writeString(name); dest.writeInt(id); } public static final Creator<Author> CREATOR=new Creator<Author>() { @Override public Author createFromParcel(Parcel source) { //從Parcel容器中讀取傳遞數(shù)據(jù)值,封裝成Parcelable對(duì)象返回邏輯層。 Author author=new Author(); author.setName(source.readString()); author.setId(source.readInt()); return author; } @Override public Author[] newArray(int size) { //創(chuàng)建一個(gè)類(lèi)型為T(mén),長(zhǎng)度為size的數(shù)組,僅一句話(huà)(return new T[size])即可。方法是供外部類(lèi)反序列化本類(lèi)數(shù)組使用。 return new Author[size]; } }; }
public class Book implements Parcelable{ private String title; private Author author; //setter & getter... @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeString(title); dest.writeParcelable(author,flags); } public static final Creator<Book> CREATOR=new Creator<Book>() { @Override public Book createFromParcel(Parcel source) { Book book=new Book(); book.setTitle(source.readString()); book.setAuthor(source.<Author>readParcelable(Author.class.getClassLoader())); return book; } @Override public Book[] newArray(int size) { return new Book[0]; } }; }
傳遞數(shù)據(jù)
Book book=new Book(); book.setTitle("Java編程思想"); Author author=new Author(); author.setId(1); author.setName("Bruce Eckel"); book.setAuthor(author); Intent intent=new Intent(this,SecondActivity.class); intent.putExtra("book",book); startActivity(intent);
接收數(shù)據(jù)
Book book=getIntent().getParcelableExtra("book"); Log.d(TAG,"book title->"+book.getTitle()); Log.d(TAG,"book author name->"+book.getAuthor().getName());
4.性能分析
經(jīng)過(guò)測(cè)試,我們得到下圖的效果
可以看出,通過(guò)轉(zhuǎn)換為字符串的速度是最慢的。Seralizable次之,Parcelable比Seralizable快10倍。所以從性能上考 慮,我們必定優(yōu)先選擇Parcelable。但是Parcelable有大量重復(fù)的模板代碼,如何簡(jiǎn)化這些操作,將是下面主要講解的內(nèi)容。
5.簡(jiǎn)化Parcel操作
如果你使用android Studio 可以通過(guò)安裝android-parcelable-intellij-plugin插件,或者自己配置模板進(jìn)行操作。
5.1 parceler
除了上面的操作,還有大量的第三方庫(kù)來(lái)簡(jiǎn)化Parcelable操作。當(dāng)然使用這些庫(kù)也許會(huì)降低Parcelable的性能。Parceler就是這樣一個(gè)庫(kù)。
Parceler使用非常簡(jiǎn)單,在定義Model時(shí)用@Parcel進(jìn)行注解,在傳遞數(shù)據(jù)的時(shí)候使用Parcels的wrap方法來(lái)包裝成一個(gè)Parcelable對(duì)象。獲取數(shù)據(jù)時(shí)用Parcels的unwrap方法來(lái)獲取對(duì)象。
@Parcel public class Author { int id; String name; //setter & getter... }
@Parcel public class Book { String title; Author author; //setter & getter }
傳遞對(duì)象
Book book=new Book(); book.setTitle("Java編程思想"); Author author=new Author(); author.setId(1); author.setName("Bruce Eckel"); book.setAuthor(author); Intent intent=new Intent(this,SecondActivity.class); intent.putExtra("book", Parcels.wrap(book)); startActivity(intent);
接收對(duì)象
Book book= Parcels.unwrap(getIntent().getParcelableExtra("book")); Log.d(TAG,"book title->"+book.getTitle()); Log.d(TAG,"book author name->"+book.getAuthor().getName());
除了Parceler之外,還有如auto-parcel,ParcelableCodeGenerator,ParcelableGenerator等第三方庫(kù),這里我將不進(jìn)行講解,有興趣的朋友,可以自行研究。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android List(集合)中的對(duì)象以某一個(gè)字段排序案例
- Android使用FontMetrics對(duì)象計(jì)算位置坐標(biāo)
- Android使用Canvas對(duì)象實(shí)現(xiàn)刮刮樂(lè)效果
- Android編程實(shí)現(xiàn)全局獲取Context及使用Intent傳遞對(duì)象的方法詳解
- Android中將Bitmap對(duì)象以PNG格式保存在內(nèi)部存儲(chǔ)中的方法
- Android中利用C++處理Bitmap對(duì)象的實(shí)現(xiàn)方法
- Android中實(shí)現(xiàn)長(zhǎng)按修改ListView對(duì)象的內(nèi)容
- Android中深入學(xué)習(xí)對(duì)象的四種引用類(lèi)型
相關(guān)文章
Android旋轉(zhuǎn)、平移、縮放和透明度漸變的補(bǔ)間動(dòng)畫(huà)
這篇文章主要實(shí)現(xiàn)Android旋轉(zhuǎn)、平移、縮放和透明度漸變的補(bǔ)間動(dòng)畫(huà),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2018-04-04Android Studio設(shè)置繪制布局時(shí)的視圖
這篇文章介紹了Android Studio設(shè)置繪制布局時(shí)視圖的方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-11-11Android?進(jìn)入Activity時(shí)如何禁止彈出軟鍵盤(pán)輸入法
這篇文章主要介紹了Android?進(jìn)入Activity時(shí)如何禁止彈出軟鍵盤(pán)輸入法,文章圍繞主題展開(kāi)具體內(nèi)容,需要的小伙伴可以參考一下2022-05-05Android獲取手機(jī)SIM卡運(yùn)營(yíng)商信息的方法
這篇文章主要介紹了Android獲取手機(jī)SIM卡運(yùn)營(yíng)商信息的方法,可獲得手機(jī)的型號(hào)、運(yùn)營(yíng)商信息及系統(tǒng)版本等,需要的朋友可以參考下2014-09-09Android實(shí)現(xiàn)歡迎頁(yè)快速啟動(dòng)的方法
這篇文章主要給大家介紹了Android實(shí)現(xiàn)歡迎頁(yè)快速啟動(dòng)的方法,文中給出了詳細(xì)的方法介紹,對(duì)大家具有一定的參考價(jià)值,需要的朋友們可以一起來(lái)學(xué)習(xí)學(xué)習(xí)。2017-02-02解決android studio android monitor打不開(kāi)的問(wèn)題
下面小編就為大家分享一篇解決android studio android monitor打不開(kāi)的問(wèn)題,具有很的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01詳解Java編程中的反射在A(yíng)ndroid開(kāi)發(fā)中的應(yīng)用
這篇文章主要介紹了詳解Java編程中的反射在A(yíng)ndroid開(kāi)發(fā)中的應(yīng)用,主要來(lái)獲取安卓系統(tǒng)的屬性值,需要的朋友可以參考下2015-07-07Jetpack?Compose狀態(tài)專(zhuān)篇精講
在今年的Google/IO大會(huì)上,亮相了一個(gè)全新的?Android?原生?UI?開(kāi)發(fā)框架-Jetpack?Compose,?與蘋(píng)果的SwiftIUI一樣,Jetpack?Compose是一個(gè)聲明式的UI框架,這篇文章主要介紹了Jetpack?Compose狀態(tài)管理2022-10-10