假设我有一个具有 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/