传输的对象需要实现序列化:有两种方式,一种是实现Serializable接口,就是原来的java方式;另外一种是android的Parcelable方式,这个性能可能好一些,我猜的,但是这在需要手动去写Parcelable接口的实现。
Serializable存数据:
- Person mPerson = new Person();
- mPerson.setName("frankie");
- mPerson.setAge(25);
- Intent mIntent = new Intent(this,ObjectTranDemo1.class);
- Bundle mBundle = new Bundle();
- mBundle.putSerializable(SER_KEY,mPerson);
- mIntent.putExtras(mBundle);
Serializable取数据:
// 获取启动该ResultActivity的Intent |
24 | Intent intent = getIntent(); |
25 | // 获取该Intent所携带的数据 |
26 | Bundle bundle = intent.getExtras(); |
27 | // 从bundle数据包中取出数据 |
28 | Person person = (Person) bundle.getSerializable( "person" ); |
- Intent mIntent = new Intent(this,ObjectTranDemo2.class);
- Bundle mBundle = new Bundle();
- mBundle.putParcelable(PAR_KEY, mBook);
- mIntent.putExtras(mBundle);
-
Book mBook = (Book)getIntent().getParcelableExtra(ObjectTranDemo.PAR_KEY);
参考1:http://blog.youkuaiyun.com/Android_Tutor/article/details/5740845