将对象从 Activity 传递到 Fragment 是通过引用传递 [英] Passing Objects from Activity to Fragments is Pass by Reference
问题描述
当我们使用 Serializable
和 Parcelable
在 activities
之间传递对象时,然后一个 对象的副本/值发送到接收 activity
(典型的按值传递行为).
When we use Serializable
and Parcelable
to pass object between activities
then a copy/value of the object is sent to the receiving activity
(Typical Pass by value behaviour).
但是为什么当我们在 Bundle
中将对象作为 Parcelable
传递给 Fragments
时,由于某种原因它们被发送为 对象的引用而不是对象的副本/值(通过引用行为).我还没有检查过Serializable
,但我相信它会是一样的.
But why is it that when we pass objects to Fragments
as Parcelable
inside Bundle
, for some reason they are sent as reference of object instead of copy/value of the object (Pass by Reference behaviour).I haven't checked this out for Serializable
but i believe it would be the same.
对象以相同的方式传递,为什么 Activity
接收 object
和 Fragment
接收它的方式不同?
The object is passed the same way so why is there a difference in how the Activity
receives the object
and how the Fragment
receives it ?
推荐答案
但是为什么当我们在 Bundle 中将对象作为 Parcelable 传递给 Fragments 时,由于某种原因它们被作为对象的引用而不是对象的副本/值发送
But why is it that when we pass objects to Fragments as Parcelable inside Bundle, for some reason they are sent as reference of object instead of copy/value of the object
这不能保证.例如,如果您进行了配置更改,或者您的流程终止但用户很快返回到您的任务,您将获得一份副本.
That is not guaranteed. For example, if you undergo a configuration change, or your process is terminated but the user returns to your task quickly, you will get a copy.
对象以同样的方式传递,那么为什么 Activity 接收对象的方式和 Fragment 接收它的方式存在差异?
The object is passed the same way so why is there a difference in how the Activity receives the object and how the Fragment receives it ?
启动活动总是涉及 IPC,因此总是涉及跨进程边界复制数据.
Starting an activity always involves IPC, and therefore always involves copying data across process boundaries.
这篇关于将对象从 Activity 传递到 Fragment 是通过引用传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!