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

    and putParcelable

    ?
  • In my situation, which method can help me putParcelableArrayList

    or putSerializable

    ?

Thanks to everyone for giving me the opportunity to grow :)

+3


source to share


5 answers


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

+7


source


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/



+2


source


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).

+2


source


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

+1


source


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.

+1


source







All Articles