Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When a parcelable object is passed through an intent, does it update with references to the original object?

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?

like image 872
user3479586 Avatar asked Jun 08 '16 05:06

user3479586


People also ask

How do I use Parcelable intent?

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);

What is a Parcelable object?

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.

What is a primary purpose of the Parcelable interface?

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.

What is the use of Parcelable in Android?

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.


1 Answers

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.

like image 164
Udayaditya Barua Avatar answered Oct 13 '22 17:10

Udayaditya Barua