如何将Enum对象添加到Android Bundle中?


当前回答

另一个选择:

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());
    }
}

其他回答

为了完整起见,这是一个完整的示例,说明如何从bundle中放入和返回enum。

给定以下enum:

enum EnumType{
    ENUM_VALUE_1,
    ENUM_VALUE_2
}

你可以把枚举放到一个bundle中:

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

并返回enum:

EnumType enumType = (EnumType)bundle.getSerializable("enum_key");

在芬兰湾的科特林:

enum class MyEnum {
  NAME, SURNAME, GENDER
}

把这个枚举放在一个Bundle中:

Bundle().apply {
  putInt(MY_ENUM_KEY, MyEnum.ordinal)
}

从Bundle中获取enum:

val ordinal = getInt(MY_ENUM_KEY, 0)
MyEnum.values()[ordinal]

完整的例子:

class MyFragment : Fragment() {

    enum class MyEnum {
        NAME, SURNAME, GENDER
    }

    companion object {
        private const val MY_ENUM_KEY = "my_enum_key"

        fun newInstance(myEnum: MyEnum) = MyFragment().apply {
            arguments = Bundle().apply {
                putInt(MY_ENUM_KEY, myEnum.ordinal)
            }
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        with(requireArguments()) {
            val ordinal = getInt(MY_ENUM_KEY, 0)
            val myEnum = MyEnum.values()[ordinal]
        }
    }
}

在Java中:

public final class MyFragment extends Fragment {
    private static final String MY_ENUM_KEY = "my_enum";

    public enum MyEnum {
        NAME,
        SURNAME,
        GENDER
    }

    public final MyFragment newInstance(MyEnum myEnum) {
        Bundle bundle = new Bundle();
        bundle.putInt(MY_ENUM_KEY, myEnum.ordinal());
        MyFragment fragment = new MyFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle arguments = this.requireArguments();
        int ordinal = arguments.getInt(MY_ENUM_KEY, 0);
        MyEnum myEnum = MyEnum.values()[ordinal];
    }
}

这对我来说很容易:

enum class MyEnum {
    FOO,
    BAR
}


val bundle = Bundle()
bundle.putAll(bundleOf("myKey", MyEnum.FOO))

// to read
val myEnum = bundle.get("myKey") as MyEnumClass

注意,如果你从onCreate得到这个,你会想使用as?防止任何空异常。

枚举是可序列化的,所以没有问题。

给定以下enum:

enum YourEnum {
  TYPE1,
  TYPE2
}

包:

// put
bundle.putSerializable("key", YourEnum.TYPE1);

// get 
YourEnum yourenum = (YourEnum) bundle.get("key");

目的:

// put
intent.putExtra("key", yourEnum);

// get
yourEnum = (YourEnum) intent.getSerializableExtra("key");

我用高棉。

companion object {

        enum class Mode {
            MODE_REFERENCE,
            MODE_DOWNLOAD
        }
}

然后放入Intent:

intent.putExtra(KEY_MODE, Mode.MODE_DOWNLOAD.name)

当你上网获得价值时:

mode = Mode.valueOf(intent.getStringExtra(KEY_MODE))