最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

什么是Bundle

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

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

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

Bundle的兩個(gè)常用方法:

  • putXxx(String key,Xxx value):Xxx表示一系列的數(shù)據(jù)類型,比如Stringint、floatParcelable、Serializable等類型,以“鍵值對(duì)”(key-value,可以理解為一個(gè)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:可以看出以下幾點(diǎn):

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

它實(shí)現(xiàn)了兩個(gè)接口,cloneable(復(fù)制)和Parcelable(打包),這就意味著他必須實(shí)現(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ù)實(shí)例化

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

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

Get/Put解析

Bundle其實(shí)就是一個(gè)容器,內(nèi)部使用了Arraymap去存儲(chǔ)數(shù)據(jù),那么就必然會(huì)提供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)換成對(duì)應(yīng)的類型返回,當(dāng)沒找到數(shù)據(jù)或發(fā)生類型轉(zhuǎn)換異常時(shí)返回缺省值(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()直接返回。當(dāng)使用構(gòu)造函數(shù)public Bundle(Bundle b)創(chuàng)建Bundle時(shí),會(huì)給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()方法中就對(duì)上述幾種情況做了不同的處理,當(dāng)mParcelledData = null時(shí),直接返回
  • 當(dāng)mParcelledData = EMPTY_PARCEL時(shí),會(huì)創(chuàng)建一個(gè)容量為1ArrayMap對(duì)象
  • 當(dāng)mParcelledData = Parcel.obtain()時(shí),則會(huì)將里面的數(shù)據(jù)讀出,并創(chuàng)建一個(gè)ArrayMap,并將數(shù)據(jù)存儲(chǔ)到ArrayMap對(duì)象里面,同時(shí)將mParcelledData回收并置為null

Bundle的使用

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

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

傳遞基本類型的對(duì)象

數(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();
//實(shí)例化一個(gè)Bundle  
Bundle bundle = intent.getExtras();
String name = bundle.getString("name");
String num = bundle.getString("num");

Activity通過Arguments給fragment類傳值

當(dāng)Activity類動(dòng)態(tài)加載fragment時(shí)可以通過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類型的對(duì)象

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

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

People people = new people();	//People類
//設(shè)置數(shù)據(jù)
String Name = "zhangSan";
String Num = "88888";
people.setName(Name);
people.setNum(Num);
//實(shí)例化一個(gè)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();
// 實(shí)例化一個(gè)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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Android控件RefreshableView實(shí)現(xiàn)下拉刷新

    Android控件RefreshableView實(shí)現(xiàn)下拉刷新

    這篇文章主要為大家詳細(xì)介紹了Android控件RefreshableView實(shí)現(xiàn)下拉刷新,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • Android中訪問證書有問題的SSL網(wǎng)頁的方法

    Android中訪問證書有問題的SSL網(wǎng)頁的方法

    在WebView里加載SSL網(wǎng)頁很正常,也沒什么難度。但如果要加載的SSL頁面的證書有問題,比如過期、信息不正確、發(fā)行機(jī)關(guān)不被信任等,WebView就會(huì)拒絕加載該網(wǎng)頁
    2014-04-04
  • Android AOP注解Annotation詳解(一)

    Android AOP注解Annotation詳解(一)

    這篇文章主要介紹了Android AOP注解Annotation詳細(xì)介紹的相關(guān)資料,Annotation是代碼里的特殊標(biāo)記,這些標(biāo)記可以在編譯、類加載、運(yùn)行時(shí)被讀取,并執(zhí)行相應(yīng)的處理,需要的朋友可以參考下
    2017-03-03
  • Android字符串資源文件format方法使用實(shí)例

    Android字符串資源文件format方法使用實(shí)例

    本文介紹了Android的資源文件values/strings.xml中如何實(shí)現(xiàn)格式化字符串,這里舉個(gè)簡單的例子供大家參考
    2013-11-11
  • 玩轉(zhuǎn)Android之Drawable的使用

    玩轉(zhuǎn)Android之Drawable的使用

    這篇文章主要為大家詳細(xì)介紹了Android之Drawable的使用方法,幫助大家系統(tǒng)的學(xué)習(xí)一下Drawable的使用,感興趣的小伙伴們可以參考一下
    2016-06-06
  • android針對(duì)json數(shù)據(jù)解析方法實(shí)例分析

    android針對(duì)json數(shù)據(jù)解析方法實(shí)例分析

    這篇文章主要介紹了android針對(duì)json數(shù)據(jù)解析方法,以實(shí)例形式較為詳細(xì)的分析了Android操作json格式數(shù)據(jù)的各種常用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-10-10
  • 如何使用SurfaceView實(shí)現(xiàn)魚兒游動(dòng)動(dòng)畫

    如何使用SurfaceView實(shí)現(xiàn)魚兒游動(dòng)動(dòng)畫

    這篇文章主要教大家如何使用SurfaceView實(shí)現(xiàn)魚兒游動(dòng)動(dòng)畫,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-04-04
  • Android編寫簡單的聊天室應(yīng)用

    Android編寫簡單的聊天室應(yīng)用

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)簡單聊天室的相關(guān)資料,具有發(fā)送表情,更改頭像等功能
    2016-06-06
  • 使用Eclipse配置android開發(fā)環(huán)境教程

    使用Eclipse配置android開發(fā)環(huán)境教程

    這篇文章主要介紹了使用Eclipse配置android開發(fā)環(huán)境教程,本文講解了下載需要用到的工具、下載完需要的工具之后開始安裝、讓Ecplise自動(dòng)安裝Android開發(fā)插件(ADT- plugin)、配置Andiord SDK路徑、測試開發(fā)一個(gè)Android項(xiàng)目等內(nèi)容,需要的朋友可以參考下
    2015-04-04
  • 掃二維碼下載apk并統(tǒng)計(jì)被掃描次數(shù)

    掃二維碼下載apk并統(tǒng)計(jì)被掃描次數(shù)

    本文主要對(duì)實(shí)現(xiàn)用戶掃描一個(gè)二維碼就能下載APP,并統(tǒng)計(jì)被掃描次數(shù)的方法進(jìn)行詳細(xì)介紹,具有一定的參考作用,下面跟著小編一起來看下吧
    2017-01-01

最新評(píng)論

尉犁县| 京山县| 彭水| 花莲市| 滦南县| 鱼台县| 葫芦岛市| 察雅县| 安国市| 牡丹江市| 莲花县| 宣恩县| 平远县| 屏山县| 旺苍县| 南江县| 兴安县| 盐亭县| 增城市| 平阴县| 静宁县| 巴中市| 南投县| 灵川县| 搜索| 乡城县| 张家界市| 德安县| 太谷县| 琼结县| 扶余县| 苗栗县| 建昌县| 临洮县| 绥宁县| 林口县| 碌曲县| 淮滨县| 积石山| 万安县| 日喀则市|