Android: Difference between Parcelable and Serializable?

匿名 (未验证) 提交于 2019-12-03 01:45:01

问题:

Why does Android provide 2 interfaces for serializing objects? Do Serializable objects interopt with Android Binder and AIDL files?

回答1:

In Android we know that we cannot just pass objects to activities. The objects must be either implements Serializable or Parcelable interface to do this.

Serializable

Serializable is a standard Java interface. You can just implement Serializable interface and add override methods.The problem with this approach is that reflection is used and it is a slow process. This method create a lot of temporary objects and cause quite a bit of garbage collection. Serializable interface is easier to implement.

Look at the example below (Serializable)

//MyObjects Serializable class  import java.io.Serializable; import java.util.ArrayList; import java.util.TreeMap;  import android.os.Parcel; import android.os.Parcelable;  public class MyObjects implements Serializable {      private String name;     private int age;      public ArrayList address;      public MyObjects(String name, int age, ArrayList address) {         super();         this.name = name;         this.age = age;         this.address = address;     }      public ArrayList getAddress() {         if (!(address == null))             return address;         else             return new ArrayList();     }      public String getName() {         return name;     }      public String getAge() {         return age;     } }    //MyObjects instance MyObjects mObjects = new MyObjects("name","age","Address array here");  //Passing MyObjects instance via intent Intent mIntent = new Intent(FromActivity.this, ToActivity.class); mIntent.putExtra("UniqueKey", mObjects); startActivity(mIntent);   //Getting MyObjects instance Intent mIntent = getIntent(); MyObjects workorder = (MyObjects)    mIntent.getSerializableExtra("UniqueKey"); 

Parcelable

Parcelable process is much faster than serializable. One of the reasons for this is that we are being explicit about the serialization process instead of using reflection to infer it. It also stands to reason that the code has been heavily optimized for this purpose.

Look at the example below (Parcelable)

//MyObjects Parcelable class  import java.util.ArrayList;  import android.os.Parcel; import android.os.Parcelable;  public class MyObjects implements Parcelable {      private int age;     private String name;      private ArrayList address;      public MyObjects(String name, int age, ArrayList address) {         this.name = name;         this.age = age;         this.address = address;     }      public MyObjects(Parcel source) {         age = source.readInt();         name = source.readString();         address = source.createStringArrayList();     }      @Override     public int describeContents() {         return 0;     }      @Override     public void writeToParcel(Parcel dest, int flags) {         dest.writeInt(age);         dest.writeString(name);         dest.writeStringList(address);     }      public int getAge() {         return age;     }      public String getName() {         return name;     }      public ArrayList getAddress() {         if (!(address == null))             return address;         else             return new ArrayList();     }      public static final Creator CREATOR = new Creator() {         @Override         public MyObjects[] newArray(int size) {             return new MyObjects[size];         }          @Override         public MyObjects createFromParcel(Parcel source) {             return new MyObjects(source);         }     };  }      MyObjects mObjects = new MyObjects("name","age","Address array here");  //Passing MyOjects instance Intent mIntent = new Intent(FromActivity.this, ToActivity.class); mIntent.putExtra("UniqueKey", mObjects); startActivity(mIntent);   //Getting MyObjects instance Intent mIntent = getIntent(); MyObjects workorder = (MyObjects) mIntent.getParcelableExtra("UniqueKey");     //You can pass Arraylist of Parceble obect as below  //Array of MyObjects ArrayList mUsers;  //Passing MyOjects instance Intent mIntent = new Intent(FromActivity.this, ToActivity.class); mIntent.putParcelableArrayListExtra("UniqueKey", mUsers); startActivity(mIntent);   //Getting MyObjects instance Intent mIntent = getIntent(); ArrayList mUsers = mIntent.getParcelableArrayList("UniqueKey"); 

Conclusion.

  1. Parcelable is faster than serializable interface
  2. Parcelable interface takes more time for implemetation compared to serializable interface
  3. Serializable interface is easier to implement
  4. Serializable interface create a lot of temporary objects and cause quite a bit of garbage collection
  5. Parcelable array can be pass via Intent in android


回答2:

Serializable is a standard Java interface. You simply mark a class Serializable by implementing the interface, and Java will automatically serialize it in certain situations.

Parcelable is an Android specific interface where you implement the serialization yourself. It was created to be far more efficient that Serializable, and to get around some problems with the default Java serialization scheme.

I believe that Binder and AIDL work with Parcelable objects.

However, you can use Serializable objects in Intents.



回答3:

If you want to be a good citizen, take the extra time to implement Parcelable since it will perform 10 times faster and use less resources.

However, in most cases, the slowness of Serializable won’t be noticeable. Feel free to use it but remember that serialization is an expensive operation so keep it to a minimum.

If you are trying to pass a list with thousands of serialized objects, it is possible that the whole process will take more than a second. It can make transitions or rotation from portrait to lanscape feel very sluggish.

Source to this point: http://www.developerphil.com/parcelable-vs-serializable/



回答4:

In Parcelable, developers write custom code for marshaling and unmarshaling so it creates less garbage objects in comparison to Serialization. The performance of Parcelable over Serialization dramatically improves (around two times faster), because of this custom implementation.

Serialization is a marker interface, which implies that user cannot marshal the data according to their requirements. In Serialization, a marshaling operation is performed on a Java Virtual Machine (JVM) using the Java reflection API. This helps identify the Java object's member and behavior, but also ends up creating a lot of garbage objects. Due to this, the Serialization process is slow in comparison to Parcelable.



回答5:

I'm actually going to be the one guy advocating for the Serializable. The speed difference is not so drastic any more since the devices are far better than several years ago and also there are other, more subtle differences. See my blog post on the issue for more info.



回答6:

1. Serializable

@see http://docs.oracle.com/javase/7/docs/api/java/io/Serializable.html

Interface of what?

  • is a standard Java interface

Speed

  • slower than Parcelable

2. Parcelable

@see http://developer.android.com/reference/android/os/Parcelable.html

Interface of what?

  • is android.os interface
    • which means Google developped Parcelable for better performance on android

Speed

  • faster ( because it is optimized for usage on android development)

> In Conclusion

Be aware that Serializable is a standard Java interface, and Parcelable is for Android Development



回答7:

There is some performance issue regarding to marshaling and unmarshaling. Parcelable is twice faster than Serializable.

Please go through the following link:

http://www.3pillarglobal.com/insights/parcelable-vs-java-serialization-in-android-app-development



回答8:

Implementation of parcelable can be faster if you use paracelable plugin in android studio. search for Android Parcelable code generator



回答9:

The Serializable interface can be used the same way as the Parcelable one, resulting in (not much) better performances. Just overwrite those two methods to handle manual marshalling and unmarshalling process:

private void writeObject(java.io.ObjectOutputStream out)     throws IOException private void readObject(java.io.ObjectInputStream in)     throws IOException, ClassNotFoundException 

Still, it seems to me that when developing native Android, using the Android api is the way to go.

See :



回答10:

Parcelable much faster than serializable with Binder, because serializable use reflection and cause many GC. Parcelable is design to optimize to pass object.

Here's link to reference. http://www.developerphil.com/parcelable-vs-serializable/



回答11:

you can use the serializable objects in the intents but at the time of making serialize a Parcelable object it can give a serious exception like NotSerializableException. Is it not recommended using serializable with Parcelable . So it is better to extends Parcelable with the object that you want to use with bundle and intents. As this Parcelable is android specific so it doesn't have any side effects. :)



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!