Android: How to put an Enum in a Bundle?

zer0stimulus picture zer0stimulus · Jul 20, 2010 · Viewed 93.5k times · Source

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

Answer

miguel picture miguel · Mar 15, 2011

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