How to read and write Enum into parcel on Android?

后端 未结 6 725
旧时难觅i
旧时难觅i 2021-02-01 12:18

Here is my model class:

public enum Action {
    RETRY, SETTINGS
}

private int imageId;
private String description;
private String actionName;
private Action ac         


        
6条回答
  •  醉话见心
    2021-02-01 13:11

    The most efficient - memory efficient - bundle is created by using the ENUM ordinal value.

    Writing to Parcel dest.writeInt(enum_variable.ordinal());

    Reading from parcel enum_variable = EnumName.values()[in.readInt()];

    This should be fine unless you don't heed the documentation's admonitions:

    Parcel is not a general-purpose serialization mechanism. This class (and the corresponding Parcelable API for placing arbitrary objects into a Parcel) is designed as a high-performance IPC transport. As such, it is not appropriate to place any Parcel data in to persistent storage: changes in the underlying implementation of any of the data in the Parcel can render older data unreadable.

    In other words, you shouldn't pass Parcels between code versions because it may not work.

提交回复
热议问题