- 一种是Bundle.putSerializable(Key,Object);
- 另一种是Bundle.putParcelable(Key, Object);
- 前者是实现了Serializable接口,而后者是实现了Parcelable接口
-
代码实现
实现Serializable的实例Person
public class Person implements Serializable { private static final long serialVersionUID = -7060210544600464481L; private String name; private int age; /** get/set方法省略 **/ }
实现Pracelable的实例Book
public class Book implements Parcelable { private String bookName; private String author; private int publishTime; /** get/set方法省略 **/ public static final Parcelable.Creator
CREATOR = new Creator () { public Book createFromParcel(Parcel source) { Book mBook = new Book(); mBook.bookName = source.readString(); mBook.author = source.readString(); mBook.publishTime = source.readInt(); return mBook; } public Book[] newArray(int size) { return new Book[size]; } }; public int describeContents() { return 0; } public void writeToParcel(Parcel parcel, int flags) { parcel.writeString(bookName); parcel.writeString(author); parcel.writeInt(publishTime); } }
>Activity的调用传输Serializeable与Pacelable的实例 ```java public class ObjectTranDemo extends Activity implements OnClickListener { public final static String SER_KEY = "com.tutor.objecttran.ser"; public final static String PAR_KEY = "com.tutor.objecttran.par"; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); /** 省略组件绑定表调用代码 **/ } //Serializeable传递对象的方法 public void SerializeMethod(){ Person mPerson = new Person(); mPerson.setName("frankie"); mPerson.setAge(25); Intent mIntent = new Intent(this,ObjectTranDemo1.class); Bundle mBundle = new Bundle(); /** 以下是Serializable传输方法 **/ mBundle.putSerializable(SER_KEY,mPerson); mIntent.putExtras(mBundle); startActivity(mIntent); } //Pacelable传递对象方法 public void PacelableMethod(){ Book mBook = new Book(); mBook.setBookName("Android Tutor"); mBook.setAuthor("Frankie"); mBook.setPublishTime(2010); Intent mIntent = new Intent(this,ObjectTranDemo2.class); Bundle mBundle = new Bundle(); /** 以下是Parcelable传输方法 **/ mBundle.putParcelable(PAR_KEY, mBook); mIntent.putExtras(mBundle); startActivity(mIntent); } } }
接收Serializable的Activity
public class ObjectTranDemo1 extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
TextView mTextView = new TextView(this); Person mPerson = (Person)getIntent().getSerializableExtra(ObjectTranDemo.SER_KEY); mTextView.setText("You name is: " + mPerson.getName() + "/n"+ "You age is: " + mPerson.getAge()); setContentView(mTextView);
} }
> 接收Parcelable的Activity ```java public class ObjectTranDemo2 extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView mTextView = new TextView(this); Book mBook = (Book)getIntent().getParcelableExtra(ObjectTranDemo.PAR_KEY); mTextView.setText("Book name is: " + mBook.getBookName()+"/n"+ "Author is: " + mBook.getAuthor() + "/n" + "PublishTime is: " + mBook.getPublishTime()); setContentView(mTextView); } }
版权归属:
eoser
许可协议:
本文使用《署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)》协议授权
评论区