Android中傳值Intent與Bundle的區(qū)別小結(jié)

前言

創(chuàng)新互聯(lián)成立于2013年,先為淮北等服務(wù)建站,淮北等地企業(yè),進行企業(yè)商務(wù)咨詢服務(wù)。為淮北企業(yè)網(wǎng)站制作PC+手機+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。

Bundle 翻譯成中文的意思是“捆綁”,常用在Activity間傳遞參數(shù),之前一開始并不太待見,原因是Intent本身就可以傳遞,Intent.putExtra("key", value),為何還要用Bundle呢?

正巧小伙伴問Android傳值Intent和Bundle區(qū)別,特此總結(jié)下:

Intent與Bundle在傳值上的區(qū)別

首先從使用上:

Intent方式:

假設(shè)需要將數(shù)據(jù)從頁面A傳遞到B,然后再傳遞到C。

A頁面中:

 Intent intent=new Intent(MainActivity.this,BActivity.class);
 intent.putExtra("String","MainActivity中的值");
 intent.putExtra("int",11);
 startActivity(intent);

B頁面中:

需要先在B頁面中接收數(shù)據(jù)

 Intent intent = getIntent();
 string = intent.getStringExtra("String");
 key = intent.getIntExtra("int",0);

然后再發(fā)數(shù)據(jù)到C頁面

 Intent intent=new Intent(BActivity.this,CActivity.class);
 intent.putExtra("String1",string);
 intent.putExtra("int1",key);
 intent.putExtra("boolean",true);
 startActivity(intent);

可以看到,使用的時候不方便的地方是需要在B頁面將數(shù)據(jù)一條條取出來然后再一條條傳輸給C頁面。

而使用Bundle的話,在B頁面可以直接取出傳輸?shù)腂undle對象然后傳輸給C頁面。

Bundle方式:

A頁面中:

 Intent intent = new Intent(MainActivity.this, BActivity.class);
 Bundle bundle = new Bundle();
 bundle.putString("String","MainActivity中的值");
 bundle.putInt("int",11);
 intent.putExtra("bundle",bundle);
 startActivity(intent);

在B頁面接收數(shù)據(jù):

Intent intent = getIntent();
bundle=intent.getBundleExtra("bundle");

然后在B頁面中發(fā)送數(shù)據(jù):

 Intent intent=new Intent(BActivity.this,CActivity.class);
 //可以傳給CActivity額外的值
 bundle.putBoolean("boolean",true);
 intent.putExtra("bundle1",bundle);
 startActivity(intent);

總結(jié):

Bundle可對對象進行操作,而Intent是不可以。Bundle相對于Intent擁有更多的接口,用起來比較靈活,但是使用Bundle也還是需要借助Intent才可以完成數(shù)據(jù)傳遞總之,Bundle旨在存儲數(shù)據(jù),而Intent旨在傳值。

然后看下intent的put方法源碼:

 public @NonNull Intent putExtra(String name, Parcelable value) {
  if (mExtras == null) {
   mExtras = new Bundle();
  }
  mExtras.putParcelable(name, value);
  return this;
 }

可以看到其實內(nèi)部也是使用的Bundle來傳輸?shù)臄?shù)據(jù)。

題外話

為什么Bundle不直接使用Hashmap代替呢?

Bundle內(nèi)部是由ArrayMap實現(xiàn)的,ArrayMap的內(nèi)部實現(xiàn)是兩個數(shù)組,一個int數(shù)組是存儲對象數(shù)據(jù)對應(yīng)下標(biāo),一個對象數(shù)組保存key和value,內(nèi)部使用二分法對key進行排序,所以在添加、刪除、查找數(shù)據(jù)的時候,都會使用二分法查找,只適合于小數(shù)據(jù)量操作,如果在數(shù)據(jù)量比較大的情況下,那么它的性能將退化。而HashMap內(nèi)部則是數(shù)組+鏈表結(jié)構(gòu),所以在數(shù)據(jù)量較少的時候,HashMap的Entry Array比ArrayMap占用更多的內(nèi)存。因為使用Bundle的場景大多數(shù)為小數(shù)據(jù)量,我沒見過在兩個Activity之間傳遞10個以上數(shù)據(jù)的場景,所以相比之下,在這種情況下使用ArrayMap保存數(shù)據(jù),在操作速度和內(nèi)存占用上都具有優(yōu)勢,因此使用Bundle來傳遞數(shù)據(jù),可以保證更快的速度和更少的內(nèi)存占用。

另外一個原因,則是在Android中如果使用Intent來攜帶數(shù)據(jù)的話,需要數(shù)據(jù)是基本類型或者是可序列化類型,HashMap使用Serializable進行序列化,而Bundle則是使用Parcelable進行序列化。而在Android平臺中,更推薦使用Parcelable實現(xiàn)序列化,雖然寫法復(fù)雜,但是開銷更小,所以為了更加快速的進行數(shù)據(jù)的序列化和反序列化,系統(tǒng)封裝了Bundle類,方便我們進行數(shù)據(jù)的傳輸。

好了,以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對創(chuàng)新互聯(lián)的支持。

分享名稱:Android中傳值Intent與Bundle的區(qū)別小結(jié)
本文路徑:http://muchs.cn/article0/phocoo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化、網(wǎng)站改版、域名注冊品牌網(wǎng)站設(shè)計、網(wǎng)站設(shè)計網(wǎng)站排名

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護公司