Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Parcelable and Serializable

So i know it is recommended to use Parcelable instead of Serializable in android, because it is faster.

My question is: is that impossible to avoid using Serializable right?

If I have a custom object i want to serialize, let's say I have the following class definition

public class Person {    String name;    int Age;    ...    .... } 

Making this parcelable is easy, because the Person class contains the types parcel.write*() supports, i.e. there is parcel.writeString and parcel.writeInt

Now, what if the Person class is the following:

public class PersonTwo {    MyCustomObj customObj;    String name;    int Age;    ...    .... } 

How am I suppose to parcel the MyCustomObj object?? It seems I need to use serializable again? but again, I thought it is SLOW to use serializable, and seems we have no choice but to use it in this case.

I don't understand

can someone tell me how I would parcel PersonTwo in this case?

like image 534
user1118019 Avatar asked Feb 17 '12 04:02

user1118019


People also ask

What is difference between Parcelable and serializable in Android?

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.

Which is better Parcelable or serializable?

Parcel able is faster than serializable. Parcel able is going to convert object to byte stream and pass the data between two activities. Writing parcel able code is little bit complex compare to serialization. It doesn't create more temp objects while passing the data between two activities.

What is a Parcelable in Android?

A Parcelable is the Android implementation of the Java Serializable. It assumes a certain structure and way of processing it. This way a Parcelable can be processed relatively fast, compared to the standard Java serialization.

Is serializable part of Android SDK?

Serializable is a standard Java interface. It is not a part of the Android SDK.


2 Answers

The link given by Ajay is the exact what you are looking for, how you can do it. Well, what you can do is implement Parcelable to your CustomObject1 and create a Parcelable class for it and then you can use that Parcelable class to Parcel it inside another Parcelable class that will Parcel both the CustomObjects.

public class CustomObject1 implements Parcelable {     // parcelable code CustomObject1 }  public class CustomObject2 implements Parcelable {      private CustomObject1 obj1;     // add  CustomObject1 here with getter setter    // parcelable code for CustomObject2       @Override     public void writeToParcel(Parcel dest, int flags) {         dest.writeParcelable(obj1, flags);     }       private void readFromParcel(Parcel in) {         obj1 = in.readParcelable(CustomObject1.class.getClassLoader());     }   ............ } 
like image 93
Lalit Poptani Avatar answered Sep 19 '22 05:09

Lalit Poptani


You need to make MyCustomObj parcelable.

like image 36
Sofi Software LLC Avatar answered Sep 18 '22 05:09

Sofi Software LLC