Is parcelable object passed through bundle same as original one?

拜拜、爱过 提交于 2019-12-10 17:33:32

问题


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");


来源:https://stackoverflow.com/questions/33495153/is-parcelable-object-passed-through-bundle-same-as-original-one

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!