Android:您可以使用 Parcelable 传递自定义对象,然后更改其内容吗?
如果我要使用 Parcelable using 将自定义对象传递到另一个活动/服务
ObjectA obj = new ObjectA();
// Set values etc.
Intent i = new Intent(this, MyActivity.class);
i.putExtra("com.package.ObjectA", obj);
startActivity(i);
,然后使用在新活动/服务中读取它
Bundle b = getIntent().getExtras();
ObjectA obj =b.getParcelable("com.package.ObjectA");
,然后更改第一个活动中的对象字段的值。
第二个活动中的对象会反映这一点,还是充当第一个对象的克隆并且永远不会改变?
如果第二个对象永远不会改变,实现这种行为的最简单方法是什么?
提前致谢
if i were to pass a custom object to another activity/service using Parcelable using
ObjectA obj = new ObjectA();
// Set values etc.
Intent i = new Intent(this, MyActivity.class);
i.putExtra("com.package.ObjectA", obj);
startActivity(i);
and then read it in the new activity/service using
Bundle b = getIntent().getExtras();
ObjectA obj =b.getParcelable("com.package.ObjectA");
and were to then change the values of the objects fields in the first activity.
would the object in the second activity reflect this or does it act as a clone of the first object and never change?
if the second object never changes what would be the simplest way to implement this sort of behavior?
thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,更改不会反映到原始对象中。如果您愿意,您可能会说它充当第一个对象的克隆。如果您需要返回修改后的对象,请这样做。请参阅 setResult,您可以向其传递一个充满返回数据的 Intent(在您的情况下是修改后的对象)。将其与 startActivityForResult 结合使用。
No, changes will not be reflected into the original object. You may say that it acts as a clone of the first object, if you want. If you need to return a modified object, then do so. See about setResult, to which you may pass an Intent filled of return data (the modified object, in your case). Use it in conjunction with startActivityForResult.