MikeS
MikeS

Reputation: 3921

Fragments, Parcelable Objects, and the Back Stack

Let's assume I have Fragment 1 and Fragment 2.

Fragment 1 creates a Person object and sets the name property to "George".

Fragment 1 then creates Fragment 2, passes the Person object as a Parcelable, then pushes Fragment 2 onto the back stack.

Fragment 2 reads in the parcelable. Later, Fragment 2 changes the name of the Person to Nancy.

The user hits the back button, popping Fragment 2 off the stack and returning to Fragment 1.

What is the Person object's name property now that we are back in Fragment 1? Is it set to George or Nancy?

Upvotes: 1

Views: 555

Answers (1)

bazyle
bazyle

Reputation: 776

It seems that in Fragment2 what you get is actually a reference to the parcelable object from Fragment1. This way, you can make the conclusion that you're modifying the same object.

Android docs:

An unusual feature of Parcel is the ability to read and write active objects. For these objects the actual contents of the object is not written, rather a special token referencing the object is written. When reading the object back from the Parcel, you do not get a new instance of the object, but rather a handle that operates on the exact same object that was originally written.

If it helps you know what to do ;)

Cheers!

Upvotes: 3

Related Questions