android - 当一个 parcelable 对象通过一个 intent 传递时,它会更新对原始对象的引用吗?

标签 android android-intent

假设我有一个具有 pricetag 属性的 Product 对象。我还有一个商店列表,每个商店都有自己的产品列表。

具体来说,产品 p 的价格为 9.99 美元,商店列表中的商店 s 在其产品列表中有 p。

我在 android Activity 中有这个商店列表,我通过 Intent 将 Product p 传递给另一个 Activity ,然后在新 Activity 中更改该对象的价格。一旦我完成这个新 Activity 并返回到旧 Activity ,对该对象所做的更改是否反射(reflect)在 Store 的产品列表中?

最佳答案

不,不维护引用。包裹是:

Container for a message (data and object references) that can be sent through an IBinder. A Parcel can contain both flattened data that will be unflattened on the other side of the IPC (using the various methods here for writing specific types, or the general Parcelable interface), and references to live IBinder objects that will result in the other side receiving a proxy IBinder connected with the original IBinder in the Parcel.

如果您查看任何 parcelable,就会发现有一个 CREATOR。这从 Parcel 中创建了一个新对象。 例如:

 public static final Creator<Employee> CREATOR = new Creator<Employee>() {
    @Override
    public Employee createFromParcel(Parcel in) {
        return new Employee(in);
    }

    @Override
    public Employee[] newArray(int size) {
        return new Employee[size];
    }
};

因此,如果您想要从另一个 Activity 返回任何数据,请使用 Lawrance 提到的 startActivityForResult

关于android - 当一个 parcelable 对象通过一个 intent 传递时,它会更新对原始对象的引用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37694110/

相关文章:

android - 无法隐藏 Marshmallow 运行时权限对话框

android - 每次我尝试将任何内容上传到 Android 模拟器时出现 IOException

android - PhoneGap 原生插件生命周期

android - 如何编写将离线启动浏览器并显示 html 的 NFC 标签?

android - 如何找到我们可以在我们的应用程序中使用其他应用程序时使用的 "public" Intent

java - 如何获取从 fragment 到某个类的微调器项目位置?

android - ImageView通过代码在android中缩放

java - Android:围绕屏幕中心旋转 Canvas

java - 两个 TextView 导致一个 Activity

android - 单击电子邮件链接时未找到处理 Intent 操作的 Activity 。查看