Lets say I have a Product object that has a pricetag property. I also have a List of Stores, each with its own list of Products.
Specifically, Product p has price $9.99, and a Store s which is in the Store list had p in its product list.
I have this store List in an android activity, and I pass Product p to another activity through an intent, and then change that object's price in the new Activity. Once I finish this new Activity and return to the old one, are the changes made to that object reflected in Store s's product list?
Suppose you have a class Foo implements Parcelable properly, to put it into Intent in an Activity: Intent intent = new Intent(getBaseContext(), NextActivity. class); Foo foo = new Foo(); intent. putExtra("foo ", foo); startActivity(intent);
A Parcelable is the Android implementation of the Java Serializable. It assumes a certain structure and way of processing it. This way a Parcelable can be processed relatively fast, compared to the standard Java serialization.
The Parcelable interface adds methods to all classes you want to be able to transfer between activities. These methods are how parcelable deconstructs the object in one activity and reconstructs it in another.
Parcelable and Bundle objects are intended to be used across process boundaries such as with IPC/Binder transactions, between activities with intents, and to store transient state across configuration changes.
No the references are not maintained. A parcel is:
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.
If you look at any parcelable, there is a CREATOR
. That creates a new object out of the Parcel
.
For example:
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];
}
};
So you if want any data back from another activity, use startActivityForResult
as Lawrance mentioned.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With