0

So I encountered an issue when passing object using Parcelable to another activity

My Model :

public class PostModel implements Parcelable {

    @SerializedName("consumer")
    public ConsumerModel consumer;

    @SerializedName("attachments")
    public List<AttachmentModel> attachments;

    @SerializedName("polling_options")
    public List<PollingModel> pollingOptions;

    @SerializedName("total_like")
    public String totalLike;

    @SerializedName("total_polling")
    public String totalPolling;

    @SerializedName("question_like")
    public QuestionLikeModel questionLikeModel; 
}

I pass the parcelable object to another activity by using :

Bundle data = new Bundle();
data.putParcelable(Constants.DEFAULT_PARAM, postModel);

And retrieve by :

getIntent().getParcelableExtra(Constants.DEFAULT_PARAM);

But strangely enough, only my model QuestionLikeModel returns null, the others are fine

So, I had to do this :

data.putParcelable(Constants.DEFAULT_PARAM, postmodel);
data.putParcelable("question_like", postModel.getQuestionLikeModel());

and set My model again by using this :

QuestionLike model q = getIntent().getParcelableExtra("question_like");
postModel.setQuestionLike(q);

To make it work

Has anyone encountered something like this before?

Karate_Dog
  • 1,265
  • 5
  • 20
  • 37
  • 1
    Are you sure the QuestionLikeModel is set at the time you're passing it on? Have you verified? Have you tried to see if you set the PostModel and get it right away, is the QuestionLikeModel persisted? If none of those work, you could implement the writing and reading from parcel yourself. – Pasi Matalamäki Sep 01 '16 at 04:30
  • 1
    i think you forget to add questionlikemodel in writeToParcel method and in constructor with parcel argument – mcd Sep 01 '16 at 04:30
  • 1
    you will get more info from android developer site. https://developer.android.com/reference/android/os/Parcelable.html – mcd Sep 01 '16 at 04:33
  • @mcd oh God I feel so stupid, thanks for reminding me. It seems I forgot to put it inside my writetoparcel – Karate_Dog Sep 01 '16 at 04:46
  • Since you have found a solution to your problem, please create an answer to your own question and accept it. That may help other developers with a similar problem. It also gets this question off the "unanswered questions list" – David Wasser Sep 01 '16 at 18:16

0 Answers0