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


当前回答

我用高棉。

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))

其他回答

最好从myEnumValue.name()中将其作为字符串传递,并从yourenum . valueof (s)中恢复,否则必须保留枚举的顺序!

更详细的解释:从枚举序数转换为枚举类型

有一点需要注意,如果你使用bundle。将一个Bundle添加到通知中,你可能会遇到以下问题:

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

...

要解决这个问题,你可以做以下事情:

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

然后可以这样使用:

// Put
Bundle bundle = new Bundle();
bundle.putInt("key", MyEnum.TYPE_0.getCode());

// Get 
MyEnum myEnum = MyEnum.fromCode(bundle.getInt("key"));

对于Intent,你可以这样使用:

意图:kotlin

FirstActivity:

val intent = Intent(context, SecondActivity::class.java)
intent.putExtra("type", typeEnum.A)
startActivity(intent)

SecondActivity:

override fun onCreate(savedInstanceState: Bundle?) {
     super.onCreate(savedInstanceState) 
     //...
     val type = (intent.extras?.get("type") as? typeEnum.Type?)
}

在芬兰湾的科特林:

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];
    }
}

为了完整起见,这是一个完整的示例,说明如何从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");