Android: How to put an Enum in a Bundle?

前端 未结 12 1413
别那么骄傲
别那么骄傲 2020-12-22 16:56

How do you add an Enum object to an Android Bundle?

相关标签:
12条回答
  • I know this is an old question, but I came with the same problem and I would like to share how I solved it. The key is what Miguel said: Enums are Serializable.

    Given the following enum:

    enum YourEnumType {
        ENUM_KEY_1, 
        ENUM_KEY_2
    }
    

    Put:

    Bundle args = new Bundle();
    args.putSerializable("arg", YourEnumType.ENUM_KEY_1);
    
    0 讨论(0)
  • 2020-12-22 17:14

    One thing to be aware of -- if you are using bundle.putSerializable for a Bundle to be added to a notification, you could run into the following issue:

    *** Uncaught remote exception!  (Exceptions are not yet supported across processes.)
        java.lang.RuntimeException: Parcelable encountered ClassNotFoundException reading a Serializable object.
    
    ...
    

    To get around this, you can do the following:

    public enum MyEnum {
        TYPE_0(0),
        TYPE_1(1),
        TYPE_2(2);
    
        private final int code;
    
        private MyEnum(int code) {
            this.code = navigationOptionLabelResId;
        }
    
        public int getCode() {
            return code;
        }
    
        public static MyEnum fromCode(int code) {
            switch(code) {
                case 0:
                    return TYPE_0;
                case 1:
                    return TYPE_1;
                case 2:
                    return TYPE_2;
                default:
                    throw new RuntimeException(
                        "Illegal TYPE_0: " + code);
            }
        }
    }
    

    Which can then be used like so:

    // Put
    Bundle bundle = new Bundle();
    bundle.putInt("key", MyEnum.TYPE_0.getCode());
    
    // Get 
    MyEnum myEnum = MyEnum.fromCode(bundle.getInt("key"));
    
    0 讨论(0)
  • 2020-12-22 17:17

    I think convert enum to int (for normal enum) and then set on bundle was been easiest way. like this code for intent:

    myIntent.PutExtra("Side", (int)PageType.Fornt);
    

    then for check state:

    int type = Intent.GetIntExtra("Side",-1);
    if(type == (int)PageType.Fornt)
    {
        //To Do
    }
    

    but not work for all enum type!

    0 讨论(0)
  • 2020-12-22 17:19

    Another option:

    public enum DataType implements Parcleable {
        SIMPLE, COMPLEX;
    
        public static final Parcelable.Creator<DataType> CREATOR = new Creator<DataType>() {
    
            @Override
            public DataType[] newArray(int size) {
                return new DataType[size];
            }
    
            @Override
            public DataType createFromParcel(Parcel source) {
                return DataType.values()[source.readInt()];
            }
        };
    
        @Override
        public int describeContents() {
            return 0;
        }
    
        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeInt(this.ordinal());
        }
    }
    
    0 讨论(0)
  • 2020-12-22 17:20

    For completeness sake, this is a full example of how to put in and get back an enum from a bundle.

    Given the following enum:

    enum EnumType{
        ENUM_VALUE_1,
        ENUM_VALUE_2
    }
    

    You can put the enum into a bundle:

    bundle.putSerializable("enum_key", EnumType.ENUM_VALUE_1);
    

    And get the enum back:

    EnumType enumType = (EnumType)bundle.getSerializable("enum_key");
    
    0 讨论(0)
  • 2020-12-22 17:21

    A simple way, assign integer value to enum

    See the following example:

    public enum MyEnum {
    
        TYPE_ONE(1), TYPE_TWO(2), TYPE_THREE(3);
    
        private int value;
    
        MyEnum(int value) {
            this.value = value;
        }
    
        public int getValue() {
            return value;
        }
    
    }
    

    Sender Side:

    Intent nextIntent = new Intent(CurrentActivity.this, NextActivity.class);
    nextIntent.putExtra("key_type", MyEnum.TYPE_ONE.getValue());
    startActivity(nextIntent);
    

    Receiver Side:

    Bundle mExtras = getIntent().getExtras();
    int mType = 0;
    if (mExtras != null) {
        mType = mExtras.getInt("key_type", 0);
    }
    
    /* OR
        Intent mIntent = getIntent();
        int mType = mIntent.getIntExtra("key_type", 0);
    */
    
    if(mType == MyEnum.TYPE_ONE.getValue())
        Toast.makeText(NextActivity.this, "TypeOne", Toast.LENGTH_SHORT).show();
    else if(mType == MyEnum.TYPE_TWO.getValue())
        Toast.makeText(NextActivity.this, "TypeTwo", Toast.LENGTH_SHORT).show();
    else if(mType == MyEnum.TYPE_THREE.getValue())
        Toast.makeText(NextActivity.this, "TypeThree", Toast.LENGTH_SHORT).show();
    else
        Toast.makeText(NextActivity.this, "Wrong Key", Toast.LENGTH_SHORT).show();
    
    0 讨论(0)
提交回复
热议问题