Android: Making a class parcelable

后端 未结 5 898
无人及你
无人及你 2020-12-15 06:32

I want to pass on object between two activities in Android which has lead me to parcelable classes. I am not trying to convert my current class but don\'t understand the Par

5条回答
  •  既然无缘
    2020-12-15 07:10

    On GitHub you can find some code I wrote some time ago for simplifying the handling of Parcelables (e.g. writing to and reading from parcels, simplifying the initializer code for CREATORs).

    Writing to a parcel:

    parcelValues(dest, name, maxSpeed, weight, wheels, color, isDriving);
    

    where color is an enum and isDriving is a boolean, for example.

    Reading from a parcel:

    // [...]
    color = (CarColor)unparcelValue(CarColor.class.getClassLoader());
    isDriving = (Boolean)unparcelValue();
    

    Creating a CREATOR for classes:

    public static final Parcelable.Creator CREATOR = 
        Parceldroid.getCreatorForClass(Car.class);
    

    ...or for enums:

    public static final Parcelable.Creator CREATOR = 
        Parceldroid.getCreatorForEnum(CarColor.class);
    

    Just take a look at the "ParceldroidExample" I added to the project.

提交回复
热议问题