6

Hi I'm beginner developer of Android :) I want to send ArrayList<Custom> value from Activity to Fragment. I know that have to use Bundle, but I don't know what's method can help me.

  1. What is different of putSerializable and putParcelable?
  2. In my situation, Which method can help me putParcelableArrayList or putSerializable?

Thanks all that give me opportunity to grow :)

Soyeon Kim
  • 608
  • 7
  • 34
  • 2
    possible duplicate of [Android: Difference between Parcelable and Serializable?](http://stackoverflow.com/questions/3323074/android-difference-between-parcelable-and-serializable) – Ivo Jul 31 '15 at 08:15
  • @IvoBeckers Oh.. Why I can't found that's question T-T Thanks! – Soyeon Kim Jul 31 '15 at 08:19

5 Answers5

12

Serializable and Parcelable are both an interface. The former is as ancient as java. It allows you to store/write an object in a persistent way. The beauty of it is that you just have to implement the interface. The latter, Parcelable, is android specific. It is faster than Serializable, but you will have to implement the method of the interface to be able to use it.

What is different of putSerializable and putParcelable

putSerializable accepts objects that implement Serializable, putParcelable accepts objects that implement Parcelable

In my situation, Which method can help me putParcelableArrayList or putSerializable?

It is difficult to say. Generally speaking I would say Parcelable

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
2

Generally speaking, Parcelable are way more faster than Serializable. Here is a great article if yout want to learn more about it http://www.developerphil.com/parcelable-vs-serializable/

skamlet
  • 693
  • 7
  • 23
2

There is no difference between putSerializable and putParcelable other than the actual value added to the map for the given key. Same goes for putParcelableArrayList. At the given key you will read a SparseArray<? extends Parcelable> .

Blackbelt explained it pretty well.

From my point of view, you don't need to use Serializable internally. I recommend using android's container for messages (a Parceable). You never know where Android SDK will go, and you do want to ensure max compatibility with ease. Remember, Android has it's own SDK (not Java's).

Laurentiu L.
  • 6,566
  • 1
  • 34
  • 60
1

For both you had to implements Parcelable or Serializable class, both allow persistence.

Parcelable is faster and you will write more function 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 is lower but you only had to add the interface on the class to make it work:

private static final long serialVersionUID = 0L; should be set inside the class that implements Serializable http://developer.android.com/reference/java/io/Serializable.html

1

Parcelable in android is an interface and every object that wants to be passed across different activities using intent has to implement this interface.This interface has two method that we have to implements: describeContent() that returns an int and writeToParcel(Parcel dest, int flags) that returns a void. More over a class that implements this interface must have a static field called CREATOR that is used by the OS to recreate the object.Clearly Parcelable array can be pass via Intent in android. This is the most elegant way to pass data between activities and exploit all the android power.

Serialization is usually used When the need arises to send your data over network or stored in files.

IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198