本文介绍如何通过Intent来实现两个组件之间传递对象。
我们采用的方法是将对象进行序列化设计,这里采用Parcelable而非Seializable。
当然对于不是很复杂的对象,可以分解其成员分别传递。
这里不讨论其优劣,详细需要采用这种方案时肯定是已经有所研究比较。
1. 首先对需要序列化的类继承自Parcelabe,实现其必要的几个方法和CREATOR静态成员:
@Override
public void writeToParcel(Parcel dest, int flags)
public static final Parcelable.Creator<CommunityComment> CREATOR
read和write Parcel的顺序是一致的。
import android.os.Parcel;
import android.os.Parcelable;
public class CommunityComment implements Parcelable {
private String commentId;
private String uid;
private String parentId;
private int likeCount;
private String createdTime;
private String content;
private int replyCount;
private String communityId;
private String imageUrl;
private String imageType;
public CommunityComment() {
}
public CommunityComment(final String commentId, final String content) {
this.commentId = commentId;
this.content = content;
}
public CommunityComment(Parcel in) {
// TODO Auto-generated constructor stub
this.commentId = in.readString();
this.uid = in.readString();
this.parentId = in.readString();
this.likeCount = in.readInt();
this.createdTime = in.readString();
this.content = in.readString();
this.replyCount = in.readInt();
this.communityId = in.readString();
this.imageUrl = in.readString();
this.imageType = in.readString();
}
public String getCommentId() {
return commentId;
}
public void setCommentId(String commentId) {
this.commentId = commentId;
}
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public String getParentId() {
return parentId;
}
public void setParentId(String parentId) {
this.parentId = parentId;
}
public int getLikeCount() {
return likeCount;
}
public void setLikeCount(int likeCount) {
this.likeCount = likeCount;
}
public String getCreatedTime() {
return createdTime;
}
public void setCreatedTime(String createdTime) {
this.createdTime = createdTime;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public int getReplyCount() {
return replyCount;
}
public void setReplyCount(int replyCount) {
this.replyCount = replyCount;
}
public String getCommunityId() {
return communityId;
}
public void setCommunityId(String communityId) {
this.communityId = communityId;
}
public String getImageUrl() {
return imageUrl;
}
public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
public String getImageType() {
return imageType;
}
public void setImageType(String imageType) {
this.imageType = imageType;
}
@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.writeString(commentId);
dest.writeString(uid);
dest.writeString(parentId);
dest.writeInt(likeCount);
dest.writeString(createdTime);
dest.writeString(content);
dest.writeInt(replyCount);
dest.writeString(communityId);
dest.writeString(imageUrl);
dest.writeString(imageType);
}
public static final Parcelable.Creator<CommunityComment> CREATOR = new Parcelable.Creator<CommunityComment>() {
public CommunityComment createFromParcel(Parcel in) {
return new CommunityComment(in);
}
public CommunityComment[] newArray(int size) {
return new CommunityComment[size];
}
};
}
2. 通过Intent发送对象(comment是需要传递的对象)
try {
Intent intent = new Intent();
intent.setClass(mContext,
CommentDetailActivity.class);
intent.putExtra(
CommentDetailActivity.EXTRA_KEY_COMMENT,
comment);
mContext.startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
}
3.接收解析该对象
public static final String EXTRA_KEY_COMMENT = "commentobj";
private CommunityComment mComment = null;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.commentdetail);
if (getIntent() == null) {
return;
}
mComment = getIntent().getExtras().getParcelable(EXTRA_KEY_COMMENT);
}