Android中Intent传递类对象提供了两种方式一种是 通过实现Serializable接口传递对象,一种是通过实现Parcelable接口传递对象。
要求被传递的对象必须实现上述2种接口中的一种才能通过Intent直接传递。
Intent中传递这2种对象的方法:
Bundle.putSerializable(Key,Object); //实现Serializable接口的对象
Bundle.putParcelable(Key, Object); //实现Parcelable接口的对象
以下以最常用的Serializable方式为例 :
Intent 同时传递对象和其他类型的数据例子:
发送方:
Intent intent1 = new Intent(this,TuiHuoTuiKuanActivity.class);
Bundle bundle1 = new Bundle();
bundle1.putSerializable("shopinfo", shopinfo);
bundle1.putString("number", 1+"");
intent1.putExtras(bundle1);
startActivity(intent1);
接收方:
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
shopinfo = (ShopInfo) bundle.getSerializable("shopinfo");
number = bundle.getString("number");