Android 不同Activity間數(shù)據(jù)的傳遞 Bundle對(duì)象的應(yīng)用
在應(yīng)用中,可能會(huì)在當(dāng)跳轉(zhuǎn)到另外一個(gè)Activity的時(shí)候需要傳遞數(shù)據(jù)過去,這時(shí)就可能用Bundle對(duì)象;
在MainActivity中,有一個(gè)導(dǎo)航至BActivity的Intent,
Intent
{
Intent intent = new Intent(Context context, Class<?> class);
//new一個(gè)Bundle對(duì)象,并將要傳遞的數(shù)據(jù)導(dǎo)入,Bunde相當(dāng)于Map<Key,Value>結(jié)構(gòu)
Bundle bundle = new Bundle();
bundle.putString("name","Livingstone");
bundle.putXXX(XXXKey, XXXValue);
//將Bundle對(duì)象添加給Intent
intent.putExtras(bundle);
//調(diào)用intent對(duì)應(yīng)的Activity
startActivity(intent);
}
在BActivity中,通過以下代碼獲取MainActivity所傳過來的數(shù)據(jù)
Bundle bundle = this.getIntent().getExtras();// 獲取傳遞過來的封裝了數(shù)據(jù)的Bundle
String name = bundle.getString("name");// 獲取name_Key對(duì)應(yīng)的Value
// 獲取值時(shí),添加進(jìn)去的是什么類型的獲取什么類型的值
--> bundle.getXXX(XXXKey);
return XXXValue
上面講述的都是一般的基本數(shù)據(jù)類型,當(dāng)需要傳遞對(duì)象的時(shí)候,可以使該對(duì)象實(shí)現(xiàn)Parcelable或者是Serializable接口;
通過Bundle.putParcelable(Key,Obj)及Bundle.putSerializable(Key,Obj)方法將對(duì)象添加到Bundle中,再將此Bundle對(duì)象添加到Intent中!
在跳轉(zhuǎn)的目標(biāo)頁面通過Intent.getParcelableExtra(Key)獲取實(shí)現(xiàn)了Parcelable的對(duì)象;
在跳轉(zhuǎn)的目標(biāo)頁面通過Intent.getSerializableExtra(Key)獲取實(shí)現(xiàn)了Serializable的對(duì)象;
今天在研究的時(shí)候發(fā)現(xiàn),Intent.putExtra(Key,Value);其實(shí)也可以傳遞數(shù)據(jù),包括上面所講的對(duì)象!
實(shí)現(xiàn)Serializable接口很簡(jiǎn)單,不再描述;
下面描述實(shí)現(xiàn)Parcelable接口:
public class Book implements Parcelable {
private String bookName;
private String author;
public static final Parcelable.Creator CREATOR = new Creator() {// 此處必須定義一個(gè)CREATOR成員變量,要不然會(huì)報(bào)錯(cuò)!
@Override
public Book createFromParcel(Parcel source) {// 從Parcel中獲取數(shù)據(jù),在獲取數(shù)據(jù)的時(shí)候需要通過此方法獲取對(duì)象實(shí)例
Book book = new Book();
book.setAuthor(source.readString());// 從Parcel讀取數(shù)據(jù),讀取數(shù)據(jù)與寫入數(shù)據(jù)的順序一致!
book.setBookName(source.readString());
return book;
}
@Override
public Book[] newArray(int size) {
return new Book[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override// 寫入Parcel
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(author);// 將數(shù)據(jù)寫入Parcel,寫入數(shù)據(jù)與讀取數(shù)據(jù)的順序一樣!
dest.writeString(bookName);
}
}
關(guān)于Parcel,大概查閱了一下描述:
一個(gè)final類,用于寫或讀各種數(shù)據(jù),所有的方法不過就是writeValue(Object)和read(ClassLoader)!(個(gè)人翻譯理解)
相關(guān)文章
Android中設(shè)置只有程序第一次運(yùn)行才顯示的界面實(shí)現(xiàn)思路
如何實(shí)現(xiàn)程序第一次運(yùn)行才顯示的界面,下面是具體的實(shí)現(xiàn)思路及步驟,有類似需求的朋友可以參考下哈2013-06-06
Android實(shí)現(xiàn)簡(jiǎn)易的鬧鐘功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)簡(jiǎn)易的鬧鐘功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-09-09
android中px和dp,px和sp之間的轉(zhuǎn)換方法
在Android開發(fā)中dp和px,sp和px之間的轉(zhuǎn)換時(shí)必不可少的。下面腳本之家小編給大家?guī)砹薬ndroid中px和dp,px和sp之間的轉(zhuǎn)換方法,感興趣的朋友一起看看吧2018-06-06
Android Native庫的加載及動(dòng)態(tài)鏈接的過程
這篇文章主要介紹了Android Native庫的加載及動(dòng)態(tài)鏈接的加載過程,需要的朋友可以參考下2018-01-01
Android2.3實(shí)現(xiàn)SD卡與U盤自動(dòng)掛載的方法
這篇文章主要介紹了Android2.3實(shí)現(xiàn)SD卡與U盤自動(dòng)掛載的方法,較為詳細(xì)的分析了Android2.3實(shí)現(xiàn)SD卡與U盤自動(dòng)掛載的具體步驟與相關(guān)技巧,需要的朋友可以參考下2016-02-02
Cocos2d-x入門教程(詳細(xì)的實(shí)例和講解)
這篇文章主要介紹了Cocos2d-x入門教程,包括詳細(xì)的實(shí)例、講解以及實(shí)現(xiàn)過程,需要的朋友可以參考下2014-04-04
Flutter事件監(jiān)聽與EventBus事件的應(yīng)用詳解
EventBus的核心是基于Streams。它允許偵聽器訂閱事件并允許發(fā)布者觸發(fā)事件,使得不同組件的數(shù)據(jù)不需要一層層傳遞,可以直接通過EventBus實(shí)現(xiàn)跨組件通訊2023-04-04
Android實(shí)現(xiàn)紅包雨動(dòng)畫效果
本篇文章主要介紹了Android實(shí)現(xiàn)紅包雨動(dòng)畫效果,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-07-07
Android 開發(fā)之旅:詳解view的幾種布局方式及實(shí)踐
這篇文章主要介紹了Android 開發(fā)之旅:詳解view的幾種布局方式及實(shí)踐,具有一定的參考價(jià)值,有需要的可以了解一下。2016-12-12

