備忘:Activity間傳遞數(shù)據(jù)

  1. 基本類型參數(shù)傳遞
    Activity間傳遞一些基本類型的數(shù)據(jù)可以直接進(jìn)行傳遞,比如
    • 直接通過(guò)intent傳遞
    //發(fā)送數(shù)據(jù)方
    Intent intent = new Intent();
    intent.putExtra("username",username);
    intent.putExtra("userpass",userpass);
    intent.setClass(ThisActivity.this,TargetActivity.class);
    startActivity(intent);

    //接受數(shù)據(jù)方
    Intent intent = getIntent();
    String name = intent.getStringExtra("username");
    String pass = intent.getStringExtra("userpass);
* 通過(guò)Bundle傳遞
    //傳入  
    Intent it = new Intent(this,typeof(OtherActivity));  
    Bundle bd = new Bundle();  
    bd.PutBoolean("isNiubi",true);  
    bd.PutInt("Id",123456);  
     bd.PutString("name","詹姆斯");  
    it.PutExtras(bd);  
    StartActivity(it);  
    //接收  
    Bundle bd = Intent.Extras;  
    int Id = bd.GetInt("Id");  
    string Name = bd.GetString("name");  
    bool niubi = bd.GetBoolean("niubi");  
  1. List對(duì)象傳遞
  //傳送
  private List<String> myList;
  Intent intent = new Intent();
  intent.putExtra("myList", (Serializable) myList);
  intent.setClass(A.this, B.class);
  startActivity(intent);  //開(kāi)始跳轉(zhuǎn)
  // 接受
  Intent intent = this.getIntent();   
  Bundle bundle = intent.getExtras();  //獲得全部數(shù)據(jù)
  myList= (List<String>) bundle.getSerializable("myList");      
  1. 一般對(duì)象傳遞
    1. 要傳遞的對(duì)象,實(shí)現(xiàn)Parcelable接口,Parcelable實(shí)現(xiàn)可以直接通過(guò)Parcelable插件實(shí)現(xiàn)。這樣不用寫(xiě)代碼

    2. 具體傳遞過(guò)程

//傳送
  Intent intent = new Intent();
  intent.putExtra("myObj", myObj);  //放入對(duì)象數(shù)據(jù)
  intent.setClass(A.this, B.class);
  startActivity(intent);  //開(kāi)始跳轉(zhuǎn)
//接受
  Intent intent = this.getIntent();    //獲得當(dāng)前的Intent
  Bundle bundle = intent.getExtras();  //獲得全部數(shù)據(jù)
  myObj = bundle.getParcelable("myObj");
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容