I don't know how to use Object serialization. I personally think that many cases where large objects need to be passed can be avoided. For example, you can write the content of multiple acitivyts to an activity. (You can also find a benefit and finish many of the activities at the same time ). But as a knowledge, I still wrote it because I haven't updated my blog for a long time.
The introduction is not prepared, or the code is simple:
public class ImageEntity implements Parcelable{ String name; String path; String path1; // first path, default path . replace with selector String path2; // second path, default path int id; boolean isSelected; public ImageEntity(String path) { this.name = path; this.path = this.path1 = this.path2 = path; this.isSelected = false; } public ImageEntity(String path1, String path2){ this.name = path1; this.path1 = path1; this.path2 = path2; } public ImageEntity(String name, String path1, String path2){ this.name = name; this.path1 = path1; this.path2 = path2; } public ImageEntity(String name, int id){ this.name = name; this.path = ""; this.id = id; this.isSelected = false; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPath() { return path; } public void setPath(String path) { this.path = path; } public String getPath1() { return path1; } public void setPath1(String path1) { this.path1 = path1; } public String getPath2() { return path2; } public void setPath2(String path2) { this.path2 = path2; } public int getId() { return id; } public void setId(int id) { this.id = id; } public boolean isSelected() { return isSelected; } public void setSelected(boolean isSelected) { this.isSelected = isSelected; } @Override public int describeContents() { // TODO Auto-generated method stub return 0; } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeString(name); dest.writeString(path); dest.writeInt(id); dest.writeByte((byte) (isSelected ? 1 : 0)); //if isSelected == true, byte == 1 } public static final Parcelable.Creator<ImageEntity> CREATOR = new Parcelable.Creator<ImageEntity>(){ @Override public ImageEntity createFromParcel(Parcel source) { return new ImageEntity(source); } @Override public ImageEntity[] newArray(int size) { // TODO Auto-generated method stub return new ImageEntity[size]; } }; private ImageEntity(Parcel parcel){ name = parcel.readString(); path = parcel.readString(); id = parcel.readInt(); isSelected = parcel.readByte() == 1; //isSelected == true if byte == 1 }}
Another one is not complete:
/** * Purpose: object that translate between activity. it may contains image-objects and may be more * Author: Slider Xiao * Created Time: Mar 12, 2013 5:04:00 PM * Update By: Slider Xiao, Mar 12, 2013 5:04:00 PM */public class ImagesIntent implements Parcelable{ private ArrayList<ImageEntity> imageEntities; public ImagesIntent(){ } public ArrayList<ImageEntity> getImageEntities() { return imageEntities; } public void setImageEntities(ArrayList<ImageEntity> imageEntities) { this.imageEntities = imageEntities; } @Override public int describeContents() { // TODO Auto-generated method stub return 0; } @Override public void writeToParcel(Parcel dest, int flags) { // TODO Auto-generated method stub dest.writeTypedList(imageEntities); } public static final Parcelable.Creator<ImagesIntent> CREATOR = new Parcelable.Creator<ImagesIntent>(){ @Override public ImagesIntent createFromParcel(Parcel source) { // TODO Auto-generated method stub return new ImagesIntent(source); } @Override public ImagesIntent[] newArray(int size) { // TODO Auto-generated method stub return new ImagesIntent[size]; } }; private ImagesIntent(Parcel parcel){ imageEntities = new ArrayList<ImageEntity>(); parcel.readTypedList(imageEntities, ImageEntity.CREATOR); }}