bundle用於場景:
在我印象中比較深刻的是,一般用於activity之間傳遞數值,也用於handler發送消息,如下:
Intent intent = new Intent();
Bundle bundle = new Bundle();
bundle.putString("KEY", "VALUE");
intent.putExtra("bundle", bundle);
intent.setClass(this, Other.class);
startActivity(intent);
Bundle bundle = new Bundle();
bundle.putString("KEY", "VALUSE");
Message msg = new Message();
msg.what = 1;
msg.obj = bundle;
Handler handler = new Handler();
handler.sendMessage(msg);
以上2個例子可以看出來Bundle其實是一個陳放信息的容器,有點像我們存儲消息的xml文件一樣,也是一個key對應一個value ,然後被被intent或者msg進行傳遞。
那麼既然是一個容器,我們怎麼傳遞和去出呢,bundle提供了很多類型的getXXX()及setXXX()的方法。下面是盜的另外一位博主的圖片啦,希望不要怪我。~^_^