Benefit of using Parcelable instead of serializing object
From "Pro Android 2"
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.
what is difference between Parcelable and Serialization used in android
whether should i used parcelable or serialization technique for sending data from one activity to other.
If you are sending a non-primitive type data/Object to another activity through the intent
you have to either Serialize
or implement Parcelable
for that object. The preferred technique is Parcelable
since it doesn't impact the performance.
is it compulsory to use one of them for sending data from one to other. / when should i use them.
It is only compulsory/used for sending non-primitive type data objects.
and the exact difference between them and performance of both of them in java aspects.
Serialization does impact the performance. For more details check this link Android Parcelable and Serializable
What are the advantages of using parcelable?
While it takes a little effort to make a custom class implement Parcelable
, the reward is that one can easily pass them as extras to a Bundle
. A Bundle
is a data container used for communication between various components in the Android universe, see also this guide on Parcelables and Bundles.
Consider the case where you want to pass an instance of a custom class from an Activity
to a Fragment
. A Fragment
has an arguments Bundle
which is customarily used for initializing the Fragment
. (I suppose because the Bundle
will still accessible if the user navigates away from the app and the whole app is stopped and restored from the back stack an hour later. On the other hand, any values passed in via the constructor will be lost if the Fragment
did not save them in yet another Bundle
in onSaveInstanceState()
)
So it's good if a value can be stored in an Bundle
. Objects belonging to classes which implement Parcelable
are of this type, together with primitive data types, some arrays, lists and serializable objects.
It's even more convenient if the value can be retrieved without having to do a class cast, that's why I prefer Parcelable
over Serializable
whenever possible:
Suppose we have two classes which implement Serializable
respectively Parcelable
.
And suppose we have a Bundle b = getArguments();
To retrieve the Serializable
value, you need the following statement
SomeSerializableClass instanceOfSerializableClass = (SomeSerializableClass)b.getSerializable("keyForSerializableValue");
whereas for the Parcelable
value you can write
SomeParcelableClass instanceOfParcelableClass = b.getParcelable("keyForParcelableValue");
In Kotlin we have one of the rare cases where the statement is longer than its Java counterpart:
val instanceOfSerializableClass: SomeSerializableClass? = b.getSerializable("keyForSerializableValue") as SomeSerializableClass
compared to
val instanceOfParcelableClass = b.getParcelable<SomeParcelableClass>("keyForParcelableValue");
Difference between Serializable, Parcelable in Android
Parcelable and Serialization are used for marshaling and unmarshaling Java objects.
More details : http://www.developerphil.com/parcelable-vs-serializable/
Is parcelable worth to implement to pass data between activities?
Parcelable
works always, all the time, on all devices. If it didn't, nothing would work. Android internals rely very heavily on Parcelable
.
Parcelable
will be a bit more efficient than Serializable
but I seriously doubt that it would have much impact on your "user experience" (unless, of course, you are using it all over the place and serializing very large and complicated objects).
If you think you are having performance issues, then I would spend my available time profiling the application and gathering empirical data about where it is spending its time. IMHO replacing Serializable
with Parcelable
is a relatively low-level implementation optimization that is likely to give you close to zero perceivable performance improvement.
Difference Between parcelable and Serialization?
You should read a nice blogpost about it : http://www.developerphil.com/parcelable-vs-serializable/
Advantages of Parcelable over JSON
I think JSON is by far the most convenient mechanism for typical POJOs; and it seems unlikely that performance should be significantly worse that with Parcelable. Parcelable implementation could be more compact; but if that is problematic, you could even compress cached JSON payloads. So I would probably try out JSON first and see how it works.
Related Topics
Android- Going Back to Previous Activity with Different Intent Value
How to Get an APK File from an Android Device
How to Create Circular Progressbar in Android
Include .So Library in APK in Android Studio
Disabling the Fullscreen Editing View for Soft Keyboard Input in Landscape
Android Edittext Delete(Backspace) Key Event
How to Set Navigation Drawer to Be Opened from Right to Left
Runtimeexception: Unable to Instantiate Application
How to Start Activity in Another Application
How to Sign an Android APK File
Android - Listview Get Item View by Position
How to Set My App as the Default Sms App
Overriding the Home Button - How to Get Rid of the Choice