I'm trying to send an object through an intent. From all the examples I've seen, it needs to be parcelable, and have all its sub-fields written to the parcel. Meaning that ultimately it is broken down to its ints, strings, etc. Is there a way to pass an object through an intent without all of this? Just send the object, have the receiving activity take it out of the intent, and then take whatever needed information it needs from it?
You might be able to get away with it, just by making the object serializable, but based on the object complexion, it could cause performance issues, other than that, there's no way to send a plain object the way you want, Intent bases its objects transfer on Inter Process Communication Protocol, which requires encode objects into more primitive values and send it to another end point and then decode them.
Hope this helps.
Regards!
You have to implement an interface called Parelable and write your object's instances inside a parcel.
Have a look at this tutorial
http://www.codexperience.co.za/post/passing-an-object-between-activities-using-an-intent
Related
I'm a C/C++ guy, and I'm quite new to Java and Android.
So I'm creating a class instance in Activity "A" and I'm trying to use this instance to Activity "B". In C/C++ you would simply pass the address (pointer) of the instance.
Now I learned that with Android I should use a Parcel, but I don't get the idea: why would I want do all that parceling/deparceling procedure when all I want to do is passing a pointer?
Please enlighten me!
This is related to the Android activity model.
Since your app may be killed by the system to recover RAM (this should only happen when it's in the background, unless the system is in a really bad situation), an object reference just won't do. It'd be completely worthless once your process dies.
The intent you use to start activities is saved by the Activity Manager, so that it can be used to recreate your activity when the user navigates back to it. That's why all the data in your intent has to be parceled.
Keep in mind that this means that your two activities will have two similar instances -- but naturally, they will not be the same object, so changes to one won't be reflected in the other.
Parcel or parcalable is an efficient and alternative way to serialize objects in Android. Its an alternative to Serialization in core Java.
Java Serialization API allows us to convert an Object to stream that we can send over the network or save it as file or store in DB for later usage.
Its a way to exchange objects between sender and receiver(Class). Receiver than deserializes this object.
In Android parcel is used to exchange objects between different activities by using Intents.
java doesn't work like c/c++, you don't work directly with the memory. If you want to pass an object from A to B, you have to set it in a Bundle, and add that bundle to the intent. if it's a custom class, let's call it C, C should implement parcellable. Java works by value, not by reference.
custom parcelable objects
how do i pass data between activities?
If I send a Parcelable object to another part of my program via LocalBroadcastManager -is the Parcelable immutable then? Immutable in the sense that the received Parcelable cant affect the object that was originally transmitted. I would guess so, because I guess Parcelable is a "deepcopy" serialization mechanism and not pass by reference, but I need to be sure. Can someone who knows, tell me if I have to worry about someone in the receiving end changing values on the received object, if I want to prevent that from affecting the original object...?
If I send a Parcelable object to another part of my program via LocalBroadcastManager -is the Parcelable immutable then?
That is not guaranteed. In fact, AFAIK, it does not happen at all for local broadcasts.
I guess Parcelable is a "deepcopy" serialization mechanism and not pass by reference
Only across process boundaries. That happens to include things like startActivity() for another one of your activities, because those involve IPC. But the Intent object -- and its extras -- is simply passed around your process with LocalBroadcastManager. I see no place where Intent would be forced to make a copy.
I would like to ask about calling an Activity with arguments. I have seen many examples about it. But I would like to ask an explanation why it is somehow "not easy" or "not simple" to create(call) an activity with custom arguments.
I've seen many posts like this and others. People passing strings but not custom objects. Yes, they are passing custom objects by implementing serializable and parceable methods but that doesn't seem efficient (I might be wrong because haven't seen it). For final declared classes, it is even more work to do.
When we pass String with putExtras(String name, String value), why is a function like putExtras(String name, Object object) not defined? wouldn't it be easier to pass any custom objects by that?
And about the complexity of the process, an explanation was given in the post here
beacuse apps are in different processes, and have separate memory
address spaces, you cant just send pointer (reference) to memory block
in your process and expect it to be available in another process.
But, still, I couldn't find a reason that satisfy me or helps me to realize what I am not seeing. Okay, let's say apps are in different processes and their memory spaces are different. So are we copying the data to be passed into another activity's memory space? or at least, shouldn't the activities which belong to same application (I believe activities in an app has something common that addresses to its app) have the common memory space so that they could easily share or pass data?
I am just trying to clarify myself about this. I know Android should not be compared with Windows development but feels like it could be better if things were simpler. I know I am missing something about this, otherwise everyone knows simpler would be better. But what is the thing that prevents passing things easier?
Thanks.
why is a function like putExtras(String name, Object object) not defined? wouldn't it be easier to pass any custom objects by that?
Because startActivity() always crosses a process boundary, and therefore whatever gets passed needs to be convertible into a byte array for transport.
So are we copying the data to be passed into another activity's memory space?
We are copying data between processes.
or at least, shouldn't the activities which belong to same application (I believe activities in an app has something common that addresses to its app) have the common memory space so that they could easily share or pass data?
Except that the primary work of startActivity() is not done by your application process, but rather by a core operating system process.
I know Android should not be compared with Windows development but feels like it could be better if things were simpler.
A better analogy is Web development, where you do not pass objects between client and server, but rather data marshalled to/from strings or byte arrays.
Just as you have figured out yourself you pass data between activities through a Bundle, like this:
Intent intent = new Intent(context, MyActivity.class);
intent.putExtra(key, "some string");
intent.putExtra(key, true);
intent.putExtra(key, 1.5f);
The thing is that Intents and Bundles are a very versatile way of passing data not only between your own activities, but also to other applications, say by launching a messaging client and providing a predefined subject and body. This is very powerful but it is also a constraint. What if you like to pass MyFooClass? Well you can't do that since an Intent is an abstract way of performing an operation, not only within your own application. One might argue that Google should have made an exception for intents between your own activities, but I really don't see this as a very big problem.
You can, as you say yourself, serialize an object using serializable interface, but you could also serialize it as a JSON object. I would also say that if you need to pass complex data structures between activities you really need to rethink your design. You should probably have some global application state (in your Application or similar class) and only pass ids, keys or similar that you can use to look up the data from your application state.
I know this question has been asked multiple number of times and i have been through a lot of these questions......almost all of these questions throw up the use of the parcelable interface for your class.
However in a couple of questions i came across a quotation:
"NOTE: Seeing Parcelable might have triggered the question, why is Android not using the
built-in Java serialization mechanism? It turns out that the Android team came to the conclusion
that the serialization in Java is far too slow to satisfy Android’s interprocess-communication
requirements. So the team built the Parcelable solution. The Parcelable approach requires
that you explicitly serialize the members of your class, but in the end, you get a much faster
serialization of your objects.
Also realize that Android provides two mechanisms that allow you to pass data to another
process. The first is to pass a bundle to an activity using an intent, and the second is to pass a
Parcelable to a service. These two mechanisms are not interchangeable and should not be
confused. That is, the Parcelable is not meant to be passed to an activity. If you want to start
an activity and pass it some data, use a bundle. Parcelable is meant to be used only as part of
an AIDL definition."
This quote can also be found in the book Pro Android 2.
Now seeing that all activities within the same application run in the same process(Every Activity in Android is a Process,or One Application is one process),unless otherwise specified in the manifest,communication within the activities of the same application is not Interprocess communication per se.So is it really faster to use the parcelable class or is it just enough to pass your object attributes through bundle via intent ?
Shedding any light on this aspect will be largely appreciated.
Cheers !!
There's a FAQ for that. :)
The short answer is that the Android team recommends three techniques for passing data between activities and services within an app: a singleton class; a public static field or method; a HashMap of WeakReferences to Objects (and you pass the key in the intent). The major issue to keep in mind is how your data is going to behave under various lifecycle events. (For instance, if the user turns the phone, by default your activities will be destroyed and recreated; your data handling method needs to be designed with that in mind.)
The Parcelable construct is designed to be very fast at passing data across application memory boundaries: within an application you are MUCH better served using Bundle because all the memory locations the data is stored in are accessible to both the sender and the receiver. Since the in-memory objects are accessible there is no need to incur the cost of reconstruction: just use the Bundle, which is really just a glorified HashMap with type-specific put/get methods.
For AIDL and IPC purposes you can't (by default) share memory locations so you need an efficient way of moving data: this is where Parcelable kicks in. Unless one of the components of your application is using the remote process capability then there is no need to use Parcelable.
From docs:
Parcelables
The Parcelable protocol provides an extremely efficient (but
low-level) protocol for objects to write and read themselves from
Parcels. You can use the direct methods writeParcelable(Parcelable,
int) and readParcelable(ClassLoader) or writeParcelableArray(T[], int)
and readParcelableArray(ClassLoader) to write or read. These methods
write both the class type and its data to the Parcel, allowing that
class to be reconstructed from the appropriate class loader when later
reading.
There are also some methods that provide a more efficient way to work
with Parcelables: writeTypedArray(T[], int), writeTypedList(List),
readTypedArray(T[], Parcelable.Creator) and readTypedList(List,
Parcelable.Creator). These methods do not write the class information
of the original object: instead, the caller of the read function must
know what type to expect and pass in the appropriate
Parcelable.Creator instead to properly construct the new object and
read its data. (To more efficient write and read a single Parceable
object, you can directly call Parcelable.writeToParcel and
Parcelable.Creator.createFromParcel yourself.)
Bundles
A special type-safe container, called Bundle, is available for
key/value maps of heterogeneous values. This has many optimizations
for improved performance when reading and writing data, and its
type-safe API avoids difficult to debug type errors when finally
marshalling the data contents into a Parcel. The methods to use are
writeBundle(Bundle), readBundle(), and readBundle(ClassLoader).
My Activity starts an IntentService that then calls a remote API. I then need to pass the resulting data back to the Activity (through a BroadcastReceiver). Within much of the rest of the app this data is encapsulated in a single class, with primitive member variables holding the data. But as the results are passed across through an Intent I'm limited in my options as I can't pass a standard Java object across the boundary. I am looking for a neat and tidy way to pass objects like this through an Intent, without requiring large amounts of repetitive code needing to be written.
Options:
Implement Parcelable on my class. Tedious to implement/maintain
Pass each member variable individually, and reconstruct the object on the other side. Tedious to implement/maintain
Use AIDL to specify the contract. Never done it before, not sure how appropriate this is.
Serialise the object and pass the String through to the Activity. Is there a String length limit for passing through an Intent? I gather there can be performance issues.
Which approach should I use, and why?
Does this remote API serialize the data in some way (JSON or XML)? Can you not hold off on the deserializing until it reaches the Activity?
Failing this I would see serialization as the best option. Parcelable is a specific type of serialization which, as you point out is a little more tricky to implementent but should be more performant if you are concerned about performance. Unless you are handling a huge amount of traffic, though, the serialization option shouldn't be too sluggish in my experience.