如何将Enum对象添加到Android Bundle中?
当前回答
有一点需要注意,如果你使用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"));
其他回答
我知道这是一个老问题,但我也遇到了同样的问题,我想分享一下我是如何解决它的。关键是Miguel所说的:枚举是可序列化的。
给定以下enum:
enum YourEnumType {
ENUM_KEY_1,
ENUM_KEY_2
}
Put:
Bundle args = new Bundle();
args.putSerializable("arg", YourEnumType.ENUM_KEY_1);
在芬兰湾的科特林:
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");
一种简单的方法,将整数值赋给enum
示例如下:
public enum MyEnum {
TYPE_ONE(1), TYPE_TWO(2), TYPE_THREE(3);
private int value;
MyEnum(int value) {
this.value = value;
}
public int getValue() {
return value;
}
}
发件人页面:
Intent nextIntent = new Intent(CurrentActivity.this, NextActivity.class);
nextIntent.putExtra("key_type", MyEnum.TYPE_ONE.getValue());
startActivity(nextIntent);
接收端:
Bundle mExtras = getIntent().getExtras();
int mType = 0;
if (mExtras != null) {
mType = mExtras.getInt("key_type", 0);
}
/* OR
Intent mIntent = getIntent();
int mType = mIntent.getIntExtra("key_type", 0);
*/
if(mType == MyEnum.TYPE_ONE.getValue())
Toast.makeText(NextActivity.this, "TypeOne", Toast.LENGTH_SHORT).show();
else if(mType == MyEnum.TYPE_TWO.getValue())
Toast.makeText(NextActivity.this, "TypeTwo", Toast.LENGTH_SHORT).show();
else if(mType == MyEnum.TYPE_THREE.getValue())
Toast.makeText(NextActivity.this, "TypeThree", Toast.LENGTH_SHORT).show();
else
Toast.makeText(NextActivity.this, "Wrong Key", Toast.LENGTH_SHORT).show();
我创建了一个Koltin扩展:
fun Bundle.putEnum(key: String, enum: Enum<*>) {
this.putString( key , enum.name )
}
inline fun <reified T: Enum<T>> Intent.getEnumExtra(key:String) : T {
return enumValueOf( getStringExtra(key) )
}
创建一个bundle并添加:
Bundle().also {
it.putEnum( "KEY" , ENUM_CLAS.ITEM )
}
并获得:
intent?.getEnumExtra< ENUM_CLAS >( "KEY" )?.let{}
推荐文章
- 如何隐藏动作栏之前的活动被创建,然后再显示它?
- 是否有一种方法以编程方式滚动滚动视图到特定的编辑文本?
- 在Android中将字符串转换为Uri
- 如何在NestedScrollView内使用RecyclerView ?
- 移动到另一个EditText时,软键盘下一步点击Android
- Android应用中的GridView VS GridLayout
- Activity和FragmentActivity的区别
- 右对齐文本在android TextView
- Java:检查enum是否包含给定的字符串?
- 从Description属性中获取Enum
- 权限拒绝:start前台需要android.permission.FOREGROUND_SERVICE
- 如何更改android操作栏的标题和图标
- 如何在c++中使用枚举
- Android Split字符串
- 让一个链接在安卓浏览器启动我的应用程序?