Android:通过Intent在两个Activity之间传递对象(Parcelable)

本文探讨如何在Android中通过Intent实现两个Activity之间的对象传递,重点在于利用Parcelable接口进行序列化。首先,需要使对象类继承Parcelable并实现相关方法及CREATOR。然后,通过Intent发送对象。在接收端解析接收到的对象。
摘要由CSDN通过智能技术生成

本文介绍如何通过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);
}


 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值