亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android開發(fā) Bundle傳值的理解與使用小結(jié)

 更新時間:2024年07月23日 10:06:52   作者:阿俊學(xué)JAVA  
這篇文章主要介紹了Android開發(fā) Bundle傳值的理解與使用小結(jié),本文通過實例代碼給大家介紹的非常詳細,感興趣的朋友一起看看吧

什么是Bundle

Bundle經(jīng)常出現(xiàn)在以下場合:

Activity狀態(tài)數(shù)據(jù)的保存與恢復(fù)涉及到的兩個回調(diào):
void onSaveInstanceState (Bundle outState)
void onCreate (Bundle savedInstanceState)
Fragment的setArguments方法:void setArguments (Bundle args)
消息機制中的Message的setData方法:void setData (Bundle data)
...

Bundle從字面上解釋為“一捆、一批、一包”,結(jié)合上述幾個應(yīng)用場合,可以知道Bundle是用來傳遞數(shù)據(jù)的。我們經(jīng)常使用BundleActivity之間傳遞數(shù)據(jù),傳遞的數(shù)據(jù)可以是boolean、byteint、longfloat、double、string等基本類型或它們對應(yīng)的數(shù)組,也可以是對象或?qū)ο髷?shù)組。當Bundle傳遞的是對象或?qū)ο髷?shù)組時,必須實現(xiàn)Serializable 或Parcelable接口。下面分別介紹Activity之間如何傳遞基本類型、傳遞對象:

Bundle的兩個常用方法:

  • putXxx(String key,Xxx value):Xxx表示一系列的數(shù)據(jù)類型,比如String、int、floatParcelable、Serializable等類型,以“鍵值對”(key-value,可以理解為一個Map<K,V>)形式保存數(shù)據(jù)
  • getXxx(String key):根據(jù)key值獲取Bundle中的value數(shù)據(jù)

Bundle源碼分析

Bundle的聲明

public final class Bundle extends BaseBundle implements Cloneable, Parcelable:可以看出以下幾點:

它使用final修飾,所以不可以被繼承

它實現(xiàn)了兩個接口,cloneable(復(fù)制)和Parcelable(打包),這就意味著他必須實現(xiàn)以下方法:

public Object clone()	//克隆數(shù)據(jù)
public int describeContents()	//寫入數(shù)據(jù)
public void writeToParcel(Parcel parcel, int flags)	//將數(shù)據(jù)打包
public void readFromParcel(Parcel parcel)	//讀取數(shù)據(jù)
public static final Parcelable.Creator<Bundle> CREATOR = new Parcelable.Creator<Bundle>()	//將打包的數(shù)據(jù)實例化

Bundle的內(nèi)存結(jié)構(gòu)

ArrayMap<String, Object> mMap = null;:使用的是ArrayMap,這個集合類存儲的也是鍵值對,但是與Hashmap不同的是,hashmap采用的是“數(shù)組+鏈表”的方式存儲,而Arraymap中使用的是兩個數(shù)組進行存儲,一個數(shù)組存儲key,一個數(shù)組存儲value,內(nèi)部的增刪改查都將會使用二分查找來進行,這個和SparseArray差不多,只不過sparseArraykey值只能是int類型的,而Arraymap可以是map型,所以在數(shù)據(jù)量不大的情況下可以使用這兩個集合代替hashmap去優(yōu)化性能

Get/Put解析

Bundle其實就是一個容器,內(nèi)部使用了Arraymap去存儲數(shù)據(jù),那么就必然會提供getput方法,由于Bundle支持的數(shù)據(jù)類型太多,這里我們就舉例布爾類型的,其他類型的方式都差不多:

getBoolean

public boolean getBoolean(String key, boolean defaultValue) {
    unparcel();
    Object o = mMap.get(key);
    if (o == null) {
        return defaultValue;
    }
    try {
        return (Boolean) o;
    } catch (ClassCastException e) {
        typeWarning(key, o, "Boolean", defaultValue, e);
        return defaultValue;
    }
}

數(shù)據(jù)讀取的邏輯就是通過keyArrayMap里讀出保存的數(shù)據(jù),并轉(zhuǎn)換成對應(yīng)的類型返回,當沒找到數(shù)據(jù)或發(fā)生類型轉(zhuǎn)換異常時返回缺省值(default-value類似null的空值)putBoolean

public void putBoolean(@Nullable String key, boolean value) {
    unparcel();
    mMap.put(key, value);
}

unparcel()

先來看下BaseBundlemParcelledData的定義:

Parcel mParcelledData = null;

在大部分情況下mParcelledData都是null,因此unparcel()直接返回。當使用構(gòu)造函數(shù)public Bundle(Bundle b)創(chuàng)建Bundle時,會給mParcelledData賦值;

oid copyInternal(BaseBundle from, boolean deep) {
    synchronized (from) {
        if (from.mParcelledData != null) {
            if (from.isEmptyParcel()) {
                mParcelledData = NoImagePreloadHolder.EMPTY_PARCEL;	//第一種取值
            } else {
                mParcelledData = Parcel.obtain();	//第二種取值
                mParcelledData.appendFrom(from.mParcelledData, 0, from.mParcelledData.dataSize());
                mParcelledData.setDataPosition(0);
            }
        } else {
            mParcelledData = null;	//第三種取值
        }
        if (from.mMap != null) {
            if (!deep) {
                mMap = new ArrayMap<>(from.mMap);
            } else {
                final ArrayMap<String, Object> fromMap = from.mMap;
                final int N = fromMap.size();
                mMap = new ArrayMap<>(N);
                for (int i = 0; i < N; i++) {
                    mMap.append(fromMap.keyAt(i), deepCopyValue(fromMap.valueAt(i)));
                }
            }
        } else {
            mMap = null;
        }
        mClassLoader = from.mClassLoader;
    }
}

從上述代碼片段可以知道mParcelledData的取值有3種情況:

mParcelledData = EMPTY_PARCEL

mParcelledData = Parcel.obtain()

mParcelledData = null

總結(jié)

  • unparcel()方法中就對上述幾種情況做了不同的處理,當mParcelledData = null時,直接返回
  • mParcelledData = EMPTY_PARCEL時,會創(chuàng)建一個容量為1ArrayMap對象
  • mParcelledData = Parcel.obtain()時,則會將里面的數(shù)據(jù)讀出,并創(chuàng)建一個ArrayMap,并將數(shù)據(jù)存儲到ArrayMap對象里面,同時將mParcelledData回收并置為null

Bundle的使用

各種常用類型的Put/Get方法

Bundle提供了,用于讀寫基本類型的數(shù)據(jù)。Bundle操作基本數(shù)據(jù)類型的API表格如下所示:

傳遞基本類型的對象

數(shù)據(jù)打包

Bundle bundle = new Bundle();
Intent intent = new Intent(MainActivity.this,Main2Activity.class);
//設(shè)置數(shù)據(jù)
String name = "zhangSan";
String num = "88888";
//把數(shù)據(jù)保存到Bundle里  
bundle.putString("name", name);
bundle.putString("num", num);
//把bundle放入intent里  
intent.putExtra("Message", bundle);
startActivity(intent);//開始打包

讀取數(shù)據(jù)

Intent intent = getIntent();
//實例化一個Bundle  
Bundle bundle = intent.getExtras();
String name = bundle.getString("name");
String num = bundle.getString("num");

Activity通過Arguments給fragment類傳值

Activity類動態(tài)加載fragment時可以通過fragmentsetArguments()傳入值,并在fragment類中通過fragmentgetArguments()方法獲得傳入的值。

數(shù)據(jù)打包

Bundle bundle = new Bundle();
fragment01 fragment01 = new fragment01();//自定義的fragment類
//設(shè)置數(shù)據(jù)
String name = "zhangSan";
String num = "88888";
//把數(shù)據(jù)保存到Bundle里  
bundle.putString("name", name);
bundle.putString("num", num);
//把bundle通過Arguments放入要傳值的fragment類里  
fragment01.setArguments(bundle);

讀取數(shù)據(jù)

String name = getArguments().getString("name");
String num = getArguments().getString("num");

傳遞Serializable類型的對象

Serializable:是一個對象序列化的接口。一個類只有實現(xiàn)了Serializable接口,它的對象才是可序列化的。因此如果要序列化某些類的對象,這些類就必須實現(xiàn)Serializable接口。而實際上,Serializable是一個空接口,沒有什么具體內(nèi)容,它的目的只是簡單的標識一個類的對象可以被序列化

打包數(shù)據(jù)

People people = new people();	//People類
//設(shè)置數(shù)據(jù)
String Name = "zhangSan";
String Num = "88888";
people.setName(Name);
people.setNum(Num);
//實例化一個Bundle  
Bundle bundle = new Bundle();
//把people數(shù)據(jù)放入到bundle中
bundle.putSerializable("people", people);
Intent intent = new Intent(MainActivity.this,Main2Activity.class);
//把bundle放入intent里  
intent.putExtras(bundle);
startActivity(intent);//開始打包

讀取數(shù)據(jù)

Intent intent = getIntent();
// 實例化一個Bundle  
Bundle bundle = intent.getExtras();
//獲取里面的people里面的數(shù)據(jù)  
People people = (people) bundle.getSerializable("people");
String name = people.getName();
String num = people.getNum();

到此這篇關(guān)于Android開發(fā) Bundle傳值的理解與使用的文章就介紹到這了,更多相關(guān)Android Bundle傳值內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論