如何使用bundle中的parcelable传递一个类对象来接收另一个活动中的所有数据?

时间:2017-11-12 11:19:05

标签: android parcelable

如何使用bundle中的parcelable传递一个类对象来接收另一个活动中的所有数据?

 public class BlogDetailVO extends BlogFeedVO implements Parcelable{

   PhotoStoryVO mCoverPic;   
   List<PhotoStoryVO> mPhotoStories;
   LocationVO mLocationVO;

public BlogDetailVO(String mId, String mName, String mDescription) {
    super(mId, mName, mDescription);
}

protected BlogDetailVO(Parcel in) {
    super(in);
    mCoverPic = in.readParcelable(PhotoStoryVO.class.getClassLoader());
    mPhotoStories = in.createTypedArrayList(PhotoStoryVO.CREATOR);
    mLocationVO = in.readParcelable(LocationVO.class.getClassLoader());
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    super.writeToParcel(dest, flags);
    dest.writeParcelable(mCoverPic, flags);
    dest.writeTypedList(mPhotoStories);
    dest.writeParcelable(mLocationVO, flags);
}

@Override
public int describeContents() {
    return 0;
}

public static final Creator<BlogDetailVO> CREATOR = new Creator<BlogDetailVO>() {
    @Override
    public BlogDetailVO createFromParcel(Parcel in) {
        return new BlogDetailVO(in);
    }

    @Override
    public BlogDetailVO[] newArray(int size) {
        return new BlogDetailVO[size];
    }
};

public PhotoStoryVO getmCoverPic() {
    return mCoverPic;
}

public void setmCoverPic(PhotoStoryVO mCoverPic) {
    this.mCoverPic = mCoverPic;
}

public List<PhotoStoryVO> getmPhotoStories() {
    return mPhotoStories;
}

public void setmPhotoStories(List<PhotoStoryVO> mPhotoStories) {
    this.mPhotoStories = mPhotoStories;
}

public LocationVO getmLocationVO() {
    return mLocationVO;
}

 public void setmLocationVO(LocationVO mLocationVO) {
    this.mLocationVO = mLocationVO;
 }
}

上面是我的类,我想从中访问另一个活动中的数据,以便如何将它从此活动发送到另一个活动以及如何在另一个活动中接收它?

2 个答案:

答案 0 :(得分:0)

调用活动

Intent intent = new Intent(fromClass.this,toClass.class).putExtra("blogDetailVOObj",blogDetailVOObj);

接收活动

BlogDetailVO blogDetailVO = getIntent().getExtras().getParcelable("blogDetailVOObj");

答案 1 :(得分:0)

将parcelableExtra放入活动中: -

DataObject dataobject = new DataObject("key", "value");

Bundle bundle = new Bundle();
bundle.putParcelable("dataobject", dataobject);
startActivity(new Intent(this, DestinationActivity.class).putExtras(bundle));

目的地活动: -

Bundle bundle = getIntent().getExtras();
if(bundle != null) {
    DataObject dataObj = bundle.getParcelable("key");
 }