Android: How to put an Enum in a Bundle?
Enums are Serializable so there is no issue.
Given the following enum:
enum YourEnum {
TYPE1,
TYPE2
}
Bundle:
// put
bundle.putSerializable("key", YourEnum.TYPE1);
// get
YourEnum yourenum = (YourEnum) bundle.get("key");
Intent:
// put
intent.putExtra("key", yourEnum);
// get
yourEnum = (YourEnum) intent.getSerializableExtra("key");
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);
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");