Context:
Android
I read a lot of examples on the net for passing datas between activities.
What I learned :
- Pass primitive datas through Intents (intent.putExtra(key,value))
- Bundle the primitive datas and put it in an intent.putExtra(key,bundle object)
- Parcelables -> for complex objects and primitives.
Before you say it, yes, I certainly missed something.
Question:
Well, it seems that there are multiple ways to achieve the same goal.
If I want to pass a custom object, a Parcelable is my man. BUT I will still have to "serialize" it, so in the end, I will also have primitives, right ?
To pass primitives, why should I use a bundle when passing directly in an intent will make it too ?
The android documentation / forums / blogs examples did not make it for me. My C background still holds me back a bit.
Why having 3 different ways to achieve one goal, by the way ?
Using Parcelable is recommended if you're passing objects with non serializables. It means you can implement a serializable representation of your objects yourself to ensure the data is what you expect to be passed back and forward between activities.
If you're passing a simple id or a string back and forward by all means use intent.putExtra()
I found this article on the topic very useful to help understand the differences and benefits between the options: http://www.developerphil.com/parcelable-vs-serializable/
Pretty much everything in Java eventually boils down to primitives.
Either way works. Use whichever makes you feel more comfortable. However, just watch out for collisions on keys (e.g., your activity, and a some base activity of yours that you inherit from, both trying to put the same thing in the same key of the
Intent
orBundle
).I am going to guess that "one goal" is "to pass data from one activity to another". That involves inter-process communication (IPC), even if the two activities are in the same process, as a core OS process is involved in the routing. For pretty much everything outside of streams from a
ContentProvider
in standard Android, IPC means that data has to be put into aParcel
, which gets converted into a byte array for passing across the process boundary.Parcelable
represents an interface that can be added to custom classes to allow them to be put into aParcel
.Bundle
is a concrete class that implementsParcelable
and represents aHashMap
-like structure, but strongly typed to things that are known to be able to go into aParcel
.Bundle
is more convenient than aParcel
for developers, in that it offers random access by key, whereParcel
does not.Intent
extras are merely aBundle
, for whichIntent
exposes its own accessor methods.AFAIK, what EpicPandaForce was referring to was the comparison between
Serializable
andParcelable
. Either can go into aBundle
or aParcel
. Of the two, all else being equal,Serializable
is slower, because it assumes that the serialized form has to be durable, able to be read in again months or years later.Parcelable
assumes that everyone is working off of the same class definition and can bypass someSerializable
overhead as a result.That being said, it's a micro-optimization.
Not everything can be made to extend
Parcelable
, notablyjava.*
classes.Integer
, for example, is notParcelable
. Yet, we would like to be able to passint
andInteger
values around. Hence,Parcel
supportsint
, as doesBundle
.AFAIK, in that comment, Mr. Marconcini was referring to "id" as a general concept, not referring to
R.id
values specifically. For example, if you are maintaining an image cache, rather than passing around actualBitmap
objects, pass around some identifier that points back to the cache.