我目前知道的Bundle的三个用处:
1. 用在Activity想Fragment传递参数,如下
Fragment fragment = new Fragment();
// 创建一个Bundle对象,用于向Fragment传入参数
Bundle args = new Bundle();
args.putInt(KEY, arguments);
// 向fragment传入参数
fragment.setArguments(args);
2.用在Handler中的message,如下
// 创建消息
Message msg = new Message();
msg.what = 0x123;
Bundle bundle = new Bundle();
bundle.putInt(UPPER_NUM , value);
msg.setData(bundle);
3.用在Activity之间传递消息,如下
Bundle data = new Bundle();
data.putInt("person", value);
// 创建一个Intent
Intent intent = new Intent(BundleTest.this, ResultActivity.class);
intent.putExtras(data);
// 启动intent对应的Activity
startActivity(intent);