Passing enum or object throgh Activity

Oct 1, 2015


Reference

Passing enum or object through an intent (the best solution)

Android: How to put an Enum in a Bundle?

Realize

  • a
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");
  • b
Given the following enum:

enum YourEnumType {
    ENUM_KEY_1, 
    ENUM_KEY_2
}
Put:

Bundle args = new Bundle();
args.putSerializable("arg", YourEnumType.ENUM_KEY_1);