The question is simple, if I make an object parcelable, and put it into a bundle and create a fragment using the bundle. Does the object get cloned or is it referenced.

The context.

Ive got an object stored/referenced in an ArrayList. Depending on the type of object in the ArrayList (polymorphism is used). I create a fragment suitable for dealing with it.

I need to also pass this object to the fragment. The fragment is used within a custom view pager. I do not wish to have duplicate objects and it seems to me parcelable clones objects.

Another method is to pass the index of the object in the ArrayList. and then get a reference to the arraylist from the fragment using getActivity().myList.get(Integer passed to ). But it doesnt seem very safe to me (ArrayList contents may change, although I simply delete everything and start again). I have also read, that you should avoid passing arguments to a fragments constructor as it may be recreated using the default no-args constructor.

(Although Im currently destroying any recreated fragments as theres some strange problem with reattaching to the correct view, another post).

Thanks

New sub-question is there a way to pass a value by reference to a fragment?

1 Answer
1

The question is simple, if I make an object parcelable, and put it into a bundle and create a fragment using the bundle. Does the object get cloned or is it referenced.

It may get cloned, if not immediately, at other points in time (e.g., when the fragment's arguments Bundle is included in the instance state).

I have also read, that you should avoid passing arguments to a fragments constructor as it may be recreated using the default no-args constructor.

Correct.

Another method is to pass the index of the object in the ArrayList. and then get a reference to the arraylist from the fragment using getActivity().myList.get(Integer passed to ). But it doesnt seem very safe to me (ArrayList contents may change, although I simply delete everything and start again).

Don't use an ArrayList. Use a HashMap with a durable key. Pass the key to the fragment. Have the fragment get the data via the key. Make sure anyone deleting this object (and thereby removing it from the HashMap) does so only when this fragment does not exist, or notifies this fragment so it knows how to handle this scenario.

That was the way I was previously handling it. just would have prefered to have done something like new ArrayList for i < ArrayList addTab(ArrayList.get(i)) ArrayList=null And have the fragments be the only area where a reference is held. But it might not make sense in the future. So I suppose ill use a hashmap. Although the question still stands, Is there a better way to pass a reference to a fragment. Thanks for the answer, Ill leave it open for another day, just incase someone else wants to add something.
–
McPJul 13 '12 at 20:30

I have tried this method however once the phone is rotated, the fragments call onCreate sooner than the activity. And so the object arnt yet initialised.
–
McPJul 13 '12 at 23:35