如何将Enum对象添加到Android Bundle中?
当前回答
在芬兰湾的科特林:
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。将一个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"));
另一个选择:
public enum DataType implements Parcleable {
SIMPLE, COMPLEX;
public static final Parcelable.Creator<DataType> CREATOR = new Creator<DataType>() {
@Override
public DataType[] newArray(int size) {
return new DataType[size];
}
@Override
public DataType createFromParcel(Parcel source) {
return DataType.values()[source.readInt()];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(this.ordinal());
}
}
最好从myEnumValue.name()中将其作为字符串传递,并从yourenum . valueof (s)中恢复,否则必须保留枚举的顺序!
更详细的解释:从枚举序数转换为枚举类型
使用包。putSerializable(String key, Serializable s)和bundle。getSerializable (String键):
enum Mode = {
BASIC, ADVANCED
}
Mode m = Mode.BASIC;
bundle.putSerializable("mode", m);
...
Mode m;
m = bundle.getSerializable("mode");
文档:http://developer.android.com/reference/android/os/Bundle.html
枚举是可序列化的,所以没有问题。
给定以下enum:
enum YourEnum {
TYPE1,
TYPE2
}
包:
// put
bundle.putSerializable("key", YourEnum.TYPE1);
// get
YourEnum yourenum = (YourEnum) bundle.get("key");
目的:
// put
intent.putExtra("key", yourEnum);
// get
yourEnum = (YourEnum) intent.getSerializableExtra("key");
推荐文章
- 如何隐藏动作栏之前的活动被创建,然后再显示它?
- 是否有一种方法以编程方式滚动滚动视图到特定的编辑文本?
- 在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字符串
- 让一个链接在安卓浏览器启动我的应用程序?