Is parcelable object passed through bundle same as

2019-06-27 12:26发布

问题:

This is something that has bothered me for a while now.

If we have some kind of Parcelable object in our activity and we pass it to fragment using Bundle, I've always thought the object that we receive in fragment is actually a new object. However, after running some tests today, it seems the object in fragment is actually same as the object in activity.

Is that correct?

EDIT: Small clarification. I don't refer to object's values. I refer to the '==' comparison.

回答1:

When you make your object Parcelable and then you pass it into another activity using Intent,something like this:

Intent i = new Intent();
i.putExtra("name_of_extra", myParcelableObject);

the object you receive in another activity or fragment is the exact object you pass it before. you can receive the object in this way:

Intent i = getIntent();
MyParcelable myParcelableObject = (MyParcelable)i.getParcelableExtra("name_of_extra");