Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intent extras aren't passed by reference?

I have a Custom Type object that implements Serializable and i'm able to sucessfully pass this object through my Activities.

Now the situation is this:

On the Activity 1 the CustomObject instance has a String property with the value "A" set and is passed to the Activity 2 as an Extra:

Intent intent = new Intent(getApplicationContext(), Activity_2.class);
intent.putExtra("CUSTOM_OBJECT", customObjectInstance);

startActivityForResult(intent, 0);

On the Activity 2, i retrieve the CustomObject from the Intent and modify the property value from "A" to "B".

When i press the back button, going from Activity 2 back to Activity 1, the value on the CustomObject's property is "A" again!

I checked the object's hash code and have confirmed that is the same instance on both Activities, and still can't figure it out why this is happening.

EDIT:

As requested, about the code where i set the property value, it is as simple as it can be:

On Activity 2:

CustomObject obj = (CustomObject) getIntent().getExtras().getSerializable("CUSTOM_OBJECT");
obj.setProperty("B");
like image 584
Aquanauta Avatar asked Mar 13 '26 18:03

Aquanauta


1 Answers

Intent's extras contain values only. You could handle parameters by reference by extending the Application class and deploying "global" variables.

Althought you express that both object's hash code is the same, i am pretty certain each Activity handles its own set of local variables. In your tests, objects in Activities A and B are independent of each other.

Hope it helps.

like image 74
Andres Avatar answered Mar 15 '26 08:03

Andres



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!