四大组件中的三大组件(Activity, Service, Receiver)都支持在Intent传递Bundle数据,由于Bundle实现了Parcelable接口,所以可以十分方便的在进程间传输,当然我们传输的数据必须能够被序列化,比如基本类型、实现了Parcelable接口的对象、实现了Serializable接口的对象以及一些Android所支持的特殊对象。
User user = new User("测试玩家", "num123");
Bundle bundle = new Bundle();
bundle.putParcelable("user", user);
Intent intent = new Intent(this, BundleActivity.class);
intent.putExtra("user", bundle);
startActivity(intent);
Bundle data = getIntent().getBundleExtra("user");
User user = data.getParcelable("user");
Log.d(TAG, "onCreate: userName = " + user.getName());
Log.d(TAG, "onCreate: id = " + user.getId());
进程间通信:
02-26 21:07:44.426 17850-17850/? D/BundleActivity: onCreate: userName =测试玩家
02-26 21:07:44.426 17850-17850/? D/BundleActivity: onCreate: id = num123