If I attach an object to a bundle and store the bundle in an intent and start another activity, does that activity work on a copy of the original object or does it use the same object but just passes along a pointer?
In Java there is no such thing as pointers. You can either pass and object by value or by reference.
CommonsWare listed the possibilities the following way:
Use remote services and AIDL to implement a remote procedure call,
effectively giving you "pass by
reference" between apps
Use Parceable and Intent extras, effectively giving you "pass by value"
between apps
So you pass your objects by value.
Related
I want to share object from one activity to another activity. I know of 2 good ways:
Using bundle: By making object's class implement Parcelable, can pass object in bundle via intent.
Using singleton pattern: saving the object instance in this class and then fetch it where ever required.
Which of the above mentioned is better or recommended way? Kindly also tell if there is some other better way.
Singleton isn't a good ideia on Activities, because it will produce memory leak (redundancy of information of an Activity).
Parcelable it's a good idea, because you will not use disk IO or Network when you create a local object from a form in the first Activity.
If you retrieve an object from Network or local database, you don't need anyone, because you can retrieve the object in the next Activity.
It took me forever to figure this out, but I'm noticing that when I pass an object in as an Extra to an Intent (in my case, an object extending Parcelable), for whatever reason it creates a new object in memory as opposed to holding onto the same object reference.
Is there a way to get it to retain the reference?
for whatever reason it creates a new object in memory as opposed to holding onto the same object reference
That depends on what you are doing with the Intent. If you are passing it as a parameter to your own method, or otherwise using it purely in-process (e.g., LocalBroadcastManager), it should not copy the extras.
However, my guess is that you are doing something like:
startActivity() and its various flavors (e.g., startActivityForResult())
startService()
bindService()
sendBroadcast() called on a Context
wrapping the Intent in a PendingIntent and using that PendingIntent somewhere (e.g., as part of a Notification)
In all of these cases, your Intent data is passed outside of your app's process to a central OS process. This requires the data to be copied. If relevant (e.g., the activity you are starting is one of your own), the Intent data is passed back from the central OS process to your process, making another copy in your process.
In these scenarios, the copying is unavoidable.
The problem is that if I update the object in the second Activity, I want it to also be updated back in the first activity.
Either:
Do not have separate activities here, or
Do not pass the object by means of an Intent extra (e.g., have a central cache of model objects, and pass the ID of the model in an extra, so the other activity can pull the object from the cache)
In order to pass an object to an Activity, object must implement Parcelable/Serializable (or JSON-encoded, or whatever casts the object to a scalar).
(I know the alternative of using singletons, statics and so on.)
Why it is not possible to give a POJO to my Activity, something like new Intent( ... , myObject) or startActivity(intent, myObject) ?
I turned #Jens's comment into a community wiki:
Your call to startActivity is passed through to ActivityManagerNative - which, in turn, communicates with the activity manager service that runs in a completely different process - thus forcing whatever you put in your Intent to be Parcelable or Serializable
I have to transfer objects between activities.
Objects are with complex structure.
I'm not sure what to do: to add bundle to intent or to store data in application class.
Which method is better /performance and garbage produced/?
The safest method is to bundle the data to pass with the Intent. This will be re-usable and avoid cluttering the global object space of the application. However this method is slightly more time consuming in that you have to implement Serializable for your custom types or create a custom Parcelable.
I have read some question here but I didn't find a solution. I have read about Parcelable, Intents, and sharing specific data within Activities from the android dev docs (both dev guide and reference).
Here's the scenario:
I have one ListActivity that fills in an object parsing an xml file, it shows a list of values, and when clicked I want to return the object that represents the item clicked to the activity that has called it, for then, call another activity with this object.
I read on how to implement Parcelable but seems not being the way. Implementing Parcelable receives a Parcel for the constructor and then reads the values from it (or at least that was what I understood). This makes no sense for me and I can't see how to implement basing on that issue. I build the object parsing the xml file, not having a Parcel.
I appreciate some clarifications on this, regards.
I believe you have three options here:
Pass some arbitrary 'id' of the object to the new activity in the intent extras, which obtains the object in the same way as the first activity. This I would recommend as it is in harmony with the way Android is designed to work.
Serialize the object using Java Serializable, then put it into the intent as an extra.
Have the object be a JSONObject and send it as a string in the intent extras.
That depends on whether the activities belong to the same process or different ones...if it is the same process then the answer is Within an application what is the best way to pass custom objects between activities? but if they belong to different processes then you are better of implementing parcelable.