Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read and write Enum into parcel on Android?

Here is my model class:

public enum Action {
    RETRY, SETTINGS
}

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

public NetworkError(int imageId, String description, String actionName, Action action ) {
    this.imageId = imageId;
    this.description = description;
    this.actionName = actionName;
    this.action = action;
}

public int getImageId() {
    return imageId;
}

public void setImageId(int imageId) {
    this.imageId = imageId;
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

public String getActionName() {
    return actionName;
}

public void setActionName(String actionName) {
    this.actionName = actionName;
}


@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeInt(this.imageId);
    dest.writeString(this.description);
    dest.writeString(this.actionName);
}

protected NetworkError(Parcel in) {
    this.imageId = in.readInt();
    this.description = in.readString();
    this.actionName = in.readString();
}

public static final Parcelable.Creator<NetworkError> CREATOR = new Parcelable.Creator<NetworkError>() {
    @Override
    public NetworkError createFromParcel(Parcel source) {
        return new NetworkError(source);
    }

    @Override
    public NetworkError[] newArray(int size) {
        return new NetworkError[size];
    }
};
like image 495
Figen Güngör Avatar asked Jul 03 '16 22:07

Figen Güngör


People also ask

Can we use enum in Android?

If you are using an Apple or Android product, you will need to download and install Remote Desktop first, you can do so from the following location: Apple Android. Enter the IP Address of your ENMU computer on campus in the 'Computer' field and click 'Connect'.

Is enum class Parcelable?

Enums are parceled with writeString using their name() because otherwise they use Java serialization (yuck!). When an enum implements Parcelable , however, it should be written using writeParcelable .

How do I save an enum in my room database?

You can consider adding a type converter for it. Looks like you should annotate your Database class with @TypeConverters annotation (and not your enum class ). Check developer.android.com/training/data-storage/room/…

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.


3 Answers

I had similar problem and my solution was:

parcel.writeString(this.questionType.name());

and for reading :

this.questionType = QuestionType.valueOf(parcel.readString());

QuestionType is enum and remember that ordering of elements matters.

like image 187
milosnkb Avatar answered Oct 12 '22 20:10

milosnkb


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.

like image 44
Brendon Whateley Avatar answered Oct 12 '22 19:10

Brendon Whateley


Any enum is serializable.

You can do this in writeToParcel(): dest.writeSerializable(action)

And in the constructor: action = (Action) in.readSerializable()

like image 35
Geekarist Avatar answered Oct 12 '22 20:10

Geekarist