Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8139729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T11:55:36+00:00 2026-06-06T11:55:36+00:00

I have a Parcelable which contains another Parcelable, both have an id. For the

  • 0

I have a Parcelable which contains another Parcelable, both have an id. For the contained Parcelable I write id 22 but it reads 20. Why?

I constructed a complete example

Parcelable1

public class Parcelable1 implements Parcelable {
public int id = 11;
public Parcelable2 parcelable2;

public Parcelable1() {
    parcelable2 = new Parcelable2(22);
}

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

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeInt(id);
    dest.writeParcelable(parcelable2, flags);
}

public static final Creator<Parcelable1> CREATOR = new Parcelable.Creator<Parcelable1>() {
    public Parcelable1 createFromParcel(Parcel source) {
        return new Parcelable1(source);
    }
    public Parcelable1[] newArray(int size) {
        return new Parcelable1[size];
    }
};

public Parcelable1(Parcel source) {
    id = source.readInt();
    
    parcelable2 = Parcelable2.CREATOR.createFromParcel(source);
}
}

Parcelable2

public class Parcelable2 implements Parcelable {
public int id = 22;

public Parcelable2(int id) {
    this.id = id;
}

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

@Override
public void writeToParcel(Parcel dest, int flags) {
    Log.d("test", "WRITE Parcelable2 id:" + id);
    
    dest.writeInt(id);
}

public static final Creator<Parcelable2> CREATOR = new Parcelable.Creator<Parcelable2>() {
    public Parcelable2 createFromParcel(Parcel source) {
        return new Parcelable2(source);
    }
    public Parcelable2[] newArray(int size) {
        return new Parcelable2[size];
    }
};

public Parcelable2(Parcel source) {
    id = source.readInt();
    
    Log.d("test", "READ Parcelable2 id:" + id);
}
}

Activity which passes the Parselable:

public class ParcelableTestActivity1 extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    
    findViewById(R.id.btn).setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(ParcelableTestActivity1.this, ParcelableTestActivity2.class);
            i.putExtra("par", new Parcelable1());
            startActivity(i);
        }
    });
}
}

The receiving activity:

public class ParcelableTestActivity2 extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main2);

    Parcelable1 p = (Parcelable1)getIntent().getParcelableExtra("par");
    
    Log.d("test", "parcelable 1 is: " + p + "id: " + p.id + " p2: " + p.parcelable2 + " p2.id: "  + p.parcelable2.id);
    
    Toast.makeText(this, "parc1 id: " + p.id + " parc2 id: " + p.parcelable2.id, Toast.LENGTH_LONG).show();
}
}

The outputs:

06-25 17:45:29.440: D/test(4107): READ Parcelable2 id:0

06-25 17:45:29.440: D/test(4107): parcelable 1 is: com.test.Parcelable1@4052e430id: 11 p2: com.test.Parcelable2@4052e6e0 p2.id: 0

06-25 17:47:19.968: D/test(4247): WRITE Parcelable2 id:22

06-25 17:47:19.988: D/test(4247): READ Parcelable2 id:20

06-25 17:47:19.988: D/test(4247): parcelable 1 is: com.test.Parcelable1@4052e238id: 11 p2: com.test.Parcelable2@4052e250 p2.id: 20

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T11:55:38+00:00Added an answer on June 6, 2026 at 11:55 am
    parcelable2 = Parcelable2.CREATOR.createFromParcel(source);
    

    That’s your problem.

    source:

    • 22
    • Parcelable2

    When you pass source into the constructor for Parcelable 2, 22 gets set as parcelable2’s id.

    You need to do this:

    id = source.readInt();
    parcelable2 = source.readParcelable(Parcelable2.class.getClassLoader())
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this: public double[][] mRoute; and I need to have it be Parcelable
I have an abstract generic class. public abstract class FieldHandlerWithData<DataType extends Parcelable> extends FieldHandler
I have a class named AllStatStruct. This class contains three attributes, which are objects
I have following class which reads and writes an array of objects from/to a
I have seen many parcelable examples so far, but for some reason I can't
I have an application that contains a main Activity, which has a very simple
I have an ExpandableListActivity (using a SimpleCursorTreeAdapter) which starts another activity when the user
I'm trying to make a Parcelable class that contains byte array. I have been
Have a SomeLib.pro file that contains: CONFIG += debug TEMPLATE = lib TARGET =
have not tested on windows. but in ubuntu when u disconnect from the network,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.