Bundle 传递收据

Bundle是一个特殊的map
它的键类型固定为String
值的类型也只有几个
发送数据:
Bundle bundle = new Bundle(); bundle.putString('sex' , '男人');
bundle.putDouble('height' , 40,38);
Intent intent = new Intent();
intent.putExtras(bundle);
intent.setClass(ThisOne.this , ThatOne.class);
startActivity(intent);
接收数据:
Bundle bundle = this.getIntent().getExtras();
String sex = bundle.getString('sex');
double height = bundle.getDouble('height');
====================================
在Message中使用Bundle传递数据
Message msg = myHandler.obtainMessage();
Bundle b = new Bundle();
b.putInt('age' , 20);
b.putString('name' , 'Jon');
msg.setData(b);
msg.sendToTarget();
class MyHandler extends Handler{
public void handleMessage(Message msg){
Bundle b = msg.getData();
int age = b.getInt('age');
String name = b.getString('name');
}
}

你可能感兴趣的:(Bundle 传递收据)