Created
September 8, 2014 14:48
-
-
Save johncarl81/a6f35bf351861d60df8d to your computer and use it in GitHub Desktop.
Example of a Parcel Enum Converter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Parcel | |
| public class Tester { | |
| @Parcel(converter = EnumExampleConverter.class) | |
| public static enum EnumExample{ | |
| ONE, | |
| TWO; | |
| } | |
| public static class EnumExampleConverter implements ParcelConverter<EnumExample>{ | |
| @Override | |
| public void toParcel(EnumExample example, android.os.Parcel parcel) { | |
| if(example == null){ | |
| parcel.writeInt(-1); | |
| } | |
| else{ | |
| parcel.writeInt(1); | |
| parcel.writeInt(example.ordinal()); | |
| } | |
| } | |
| @Override | |
| public EnumExample fromParcel(android.os.Parcel parcel) { | |
| if(parcel.readInt() == -1){ | |
| return null; | |
| } | |
| return EnumExample.values()[parcel.readInt()]; | |
| } | |
| } | |
| EnumExample example; | |
| public EnumExample getExample() { | |
| return example; | |
| } | |
| public void setExample(EnumExample example) { | |
| this.example = example; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment