Michael Eilers Smith
Michael Eilers Smith

Reputation: 8608

Does Bundle.putParcelable() keep reference to object or does it clone it?

I'm sending data between fragments like this:

public static final MyFragment newInstance( MyObject obj )
{
    MyFragment f = new MyFragment();

    // Get arguments passed in, if any
    Bundle args = f.getArguments();
    if (args == null) {
        args = new Bundle();
    }

    // Add parameters to the argument bundle
    args.putParcelable("obj", obj );          // clone or referenced? MyObject implements parcelable
    f.setArguments(args);

    return f;
}

I've found that the objects I pass are being changed uniformly. For example, when I press the back button and I go back to the last fragment, my object has the current state of the fragment I just left.

Doesn't Bundle.putParcelable() make a clone of the object?

Upvotes: 0

Views: 1321

Answers (1)

wangyif2
wangyif2

Reputation: 2863

From Android Sources:

public void putParcelable(String key, Parcelable value) {
     unparcel();
     mMap.put(key, value);
     mFdsKnown = false;
}

So no, putParcelable does not clone the object.

Upvotes: 3

Related Questions