Bundle研究

解釋:Bundle類用作於Activity之間攜帶數據傳遞,它類似於Map,用於存放key-value名值對形式的值,相對於Map,它提供了各種常用類型的putXxx()/getXxx()方法。putXXX()用於往Bundle對象放入數據,getXxx()方法用於從Bundle對象裏獲取數據。Bundle的內部實際上是使用了HashMap類型的變量來存放putXxx()方法放入的值。

 

eg: 傳值

    Bundle mbundle = new Bundle();

    mbundle.putString("name","要傳的值");//String類型的值

   mbundle.putParcelable("TravelBean",tb);//傳遞一個對象

    mbundle.putInt("type",2);//傳遞Int類型的值   

    Intent mintent = newintent(A.this,B.class);

   mintent.putExtra("bundle",mbundle);

    startActivity(mintent);

 

        得值

        Bundle mbundle =getIntent().getBundleExtra("bundle");

        If(mbundle!=null){

                String name =mbundle.getString("name");

                TravelBean tb = mbundle.getParcelable("TravelBean");

       int type =mbundle.getInt("type");

        }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章