Android Parcelable and Serializable

前端 未结 4 1760
醉酒成梦
醉酒成梦 2020-12-05 17:37

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 Serializabl

相关标签:
4条回答
  • 2020-12-05 17:58

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

    0 讨论(0)
  • 2020-12-05 18:00

    You need to make MyCustomObj parcelable.

    0 讨论(0)
  • 2020-12-05 18:00

    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.

    0 讨论(0)
  • 2020-12-05 18:15

    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());
        }
      ............
    }
    
    0 讨论(0)
提交回复
热议问题