What is the difference between putSerializable and putParcelable in Java?
Hi I'm a beginner android developer :) I want to send a ArrayList<Custom>
value from Activity
to Fragment
. I know what to use Bundle
, but I don't know which method can help me.
- What is different from
putSerializable
andputParcelable
? - In my situation, which method can help me
putParcelableArrayList
orputSerializable
?
Thanks to everyone for giving me the opportunity to grow :)
source to share
Serializable
and Parcelable
- both interfaces. The first is as old as Java. It allows you to save / write an object in a permanent way. The beauty is that you just need to implement the interface. The latter, is Parcelable
, is android specific. It's faster than Serializable
, but you will need to implement an interface method to be able to use it.
What is the difference between putSerializable and putParcelable
putSerializable
takes objects that implement Serializable
, putParcelable takes objects that implementParcelable
In my situation, which method can putParcelableArrayList or putSerializable help me?
Hard to say. Generally speaking, I would sayParcelable
source to share
Generally speaking, Parcelable
much faster than Serializable
. Here is a great article if you want to know more about this http://www.developerphil.com/parcelable-vs-serializable/
source to share
There is no difference between putSerializable and putParcelable other than the actual value added to the map for the given key. The same goes for putParcelableArrayList. This is the key you are reading SparseArray<? extends Parcelable>
.
Blackbelt explained it pretty well.
From my point of view, you don't need to use Serializable internally. I recommend using the Android container for messages (Parceable). You never know where the Android SDK will go and you want to ensure maximum compatibility with ease. Remember, Android has its own SDK (not Java).
source to share
For both, you had to implement a class Parcelable
or Serializable
, both of which are persistent.
Parcelable
is faster and you will write more functions to make it work:
describeContents()
writeToParcel(Parcel out, int flags)
Parcelable.Creator<MyClass> CREATOR = new Parcelable.Creator<MyClass>()
MyClass(Parcel in) //an constructor for Parcel object (Parcelable)
http://developer.android.com/reference/android/os/Parcelable.html
Serializable
below, but you needed to add the interface to the class for it to work:
private static final long serialVersionUID = 0L;
must be installed inside a class that implements Serializable
http://developer.android.com/reference/java/io/Serializable.html
source to share
Parcelable in android is an interface, and every object that wants to go through different actions using intent must implement this interface. the interface has two methods: one that returns an int and one that returns void. Moreover, a class that implements this interface must have a static field called , which is used by the OS to recreate the object. Obviously a Parcelable array can traverse an Intent in android. It's the most elegant way to transfer data between activities and use the full power of Android. implements: describeContent()
writeToParcel(Parcel dest, int flags)
CREATOR
Serialization is commonly used when it becomes necessary to send your data over the network or be stored in files.
source to share