欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android 不同Activity間數(shù)據(jù)的傳遞 Bundle對(duì)象的應(yīng)用

 更新時(shí)間:2013年04月19日 17:21:34   作者:  
本篇文章小編為大家介紹,Android 不同Activity間數(shù)據(jù)的傳遞 Bundle對(duì)象的應(yīng)用。需要的朋友參考下

在應(yīng)用中,可能會(huì)在當(dāng)跳轉(zhuǎn)到另外一個(gè)Activity的時(shí)候需要傳遞數(shù)據(jù)過(guò)去,這時(shí)就可能用Bundle對(duì)象;

在MainActivity中,有一個(gè)導(dǎo)航至BActivity的Intent,

Intent

復(fù)制代碼 代碼如下:

{

  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中,通過(guò)以下代碼獲取MainActivity所傳過(guò)來(lái)的數(shù)據(jù)

  Bundle bundle = this.getIntent().getExtras();// 獲取傳遞過(guò)來(lái)的封裝了數(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接口;

通過(guò)Bundle.putParcelable(Key,Obj)及Bundle.putSerializable(Key,Obj)方法將對(duì)象添加到Bundle中,再將此Bundle對(duì)象添加到Intent中!


在跳轉(zhuǎn)的目標(biāo)頁(yè)面通過(guò)Intent.getParcelableExtra(Key)獲取實(shí)現(xiàn)了Parcelable的對(duì)象;

在跳轉(zhuǎn)的目標(biāo)頁(yè)面通過(guò)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接口:

復(fù)制代碼 代碼如下:

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í)候需要通過(guò)此方法獲取對(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ù),所有的方法不過(guò)就是writeValue(Object)和read(ClassLoader)!(個(gè)人翻譯理解)

相關(guān)文章

  • Android仿微信文章懸浮窗效果的實(shí)現(xiàn)代碼

    Android仿微信文章懸浮窗效果的實(shí)現(xiàn)代碼

    這篇文章主要介紹了Android仿微信文章懸浮窗效果的實(shí)現(xiàn)代碼,需要的朋友可以參考下
    2018-10-10
  • Android 程序申請(qǐng)權(quán)限注意事項(xiàng)

    Android 程序申請(qǐng)權(quán)限注意事項(xiàng)

    本主要介紹Android 程序申請(qǐng)權(quán)限注意事項(xiàng),這里整理了相關(guān)資料,并詳細(xì)說(shuō)明如何避免開發(fā)的程序支持設(shè)備減少,有需要的小伙伴可以參考下
    2016-09-09
  • Android實(shí)現(xiàn)顯示系統(tǒng)實(shí)時(shí)時(shí)間

    Android實(shí)現(xiàn)顯示系統(tǒng)實(shí)時(shí)時(shí)間

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)顯示系統(tǒng)實(shí)時(shí)時(shí)間,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-05-05
  • Android實(shí)現(xiàn)Activity、Service與Broadcaster三大組件之間互相調(diào)用的方法詳解

    Android實(shí)現(xiàn)Activity、Service與Broadcaster三大組件之間互相調(diào)用的方法詳解

    這篇文章主要介紹了Android實(shí)現(xiàn)Activity、Service與Broadcaster三大組件之間互相調(diào)用的方法,結(jié)合實(shí)例形式詳細(xì)分析了Activity、Service與Broadcaster三大組件相互調(diào)用的原理,實(shí)現(xiàn)方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下
    2016-03-03
  • Android寫一個(gè)實(shí)時(shí)輸入框功能

    Android寫一個(gè)實(shí)時(shí)輸入框功能

    這篇文章主要介紹了Android寫一個(gè)實(shí)時(shí)輸入框功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-04-04
  • 詳解EventBus 3.x 的快速使用

    詳解EventBus 3.x 的快速使用

    這篇文章主要介紹了詳解EventBus 3.x 的快速使用,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-02-02
  • ContentProvider啟動(dòng)流程示例解析

    ContentProvider啟動(dòng)流程示例解析

    這篇文章主要為大家介紹了ContentProvider啟動(dòng)流程示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-03-03
  • Android開發(fā)Jetpack組件ViewModel使用講解

    Android開發(fā)Jetpack組件ViewModel使用講解

    這篇文章主要介紹了Android?Jetpack架構(gòu)組件?ViewModel詳解,ViewModel類讓數(shù)據(jù)可在發(fā)生屏幕旋轉(zhuǎn)等配置更改后繼續(xù)存在,ViewModel類旨在以注重生命周期的方式存儲(chǔ)和管理界面相關(guān)的數(shù)據(jù),感興趣可以來(lái)學(xué)習(xí)一下
    2022-08-08
  • Android實(shí)現(xiàn)移動(dòng)小球和CircularReveal頁(yè)面切換動(dòng)畫實(shí)例代碼

    Android實(shí)現(xiàn)移動(dòng)小球和CircularReveal頁(yè)面切換動(dòng)畫實(shí)例代碼

    這篇文章主要給大家介紹了關(guān)于利用Android如何實(shí)現(xiàn)移動(dòng)的小球和CircularReveal頁(yè)面切換動(dòng)畫的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)各位Android開發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。
    2017-09-09
  • Android Webview的postUrl與loadUrl加載頁(yè)面實(shí)例

    Android Webview的postUrl與loadUrl加載頁(yè)面實(shí)例

    這篇文章主要介紹了Android Webview的postUrl與loadUrl加載頁(yè)面實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-03-03

最新評(píng)論