2

I am passing object from an activity to a fragment as explained in the accepted answer here: Passing an Object from an Activity to a Fragment

However, my question is, when an object is passed to the fragment, is it a reference to the object? or a copy of the object?

Thanks.

Community
  • 1
  • 1
Greeso
  • 7,544
  • 9
  • 51
  • 77
  • Using that method, serializing and then deserializing, you are getting a copy of the object. – Tenfour04 Jun 13 '14 at 17:26
  • How can I pass a reference then? – Greeso Jun 13 '14 at 17:27
  • 1
    One way is to use a singleton to reference it. You can also subclass Application and add public fields to it, but Android's engineers highly discourage this. I've used the second method in an app without issue. Application is alive for the entire duration of your app's life, so a public non-static field in you Application subclass acts pretty much the same as a static field. – Tenfour04 Jun 13 '14 at 17:31

1 Answers1

-1

Intent's extras contain values only. So it is a copy of your object not it reference.

marshallino16
  • 2,645
  • 15
  • 29
  • But according to the answers here: http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value we do get references passed! I am really confused – Greeso Jun 13 '14 at 18:25
  • The question doesn't have anything in common with the Intent extras. You will use Intent and extras when you pass data to an Activity (where extras can pass not references, but values only as you mentioned). Here he will pass an object through the Fragment arguments – Leo DroidCoder Feb 03 '17 at 17:47