30

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?

Huangism
  • 16,278
  • 7
  • 48
  • 74
user1118019
  • 3,819
  • 8
  • 40
  • 46
  • 2
    I suggest you to use Serializable. According to the tests on Nexus 5 it is much faster, than Parcelable. See https://bitbucket.org/afrishman/androidserializationtest. The main thing to remember is that Serializable is slow only with default settings. If you use manual serialization, it is much, much faster. Check the link above for details. – afrish Mar 29 '15 at 16:20

4 Answers4

16

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());
    }
  ............
}
Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242
  • 2
    Thanks dude, i would have accepted both you and ajay as answer, but seems i can only pick one. anyway thank you!! – user1118019 Feb 17 '12 at 05:47
  • 1
    Yes this is spot on and how i do it. Just make every single object required, parcelable because at the end of it all, the root object will contain primitive or String types – Jono Apr 04 '13 at 23:23
2

You need to make MyCustomObj parcelable.

Sofi Software LLC
  • 3,879
  • 1
  • 36
  • 34
0

All the composite objects should also be Parcelable. In case, you want to skip an object then don't use it writeToParcel method.

Shinoo Goyal
  • 601
  • 8
  • 10
-1

I came to point where Parcelable is an issue for me. On Android 4.3, I am getting unmarhalling exception, when passing data between Activities as Parcelable. It works OK on Android 4.0, 4.2 or 4.4. It should work when changed to Serializable, even though, it is slower.

MartinC
  • 546
  • 11
  • 26