我有一个活动,启动时需要访问两个不同的数组列表。两个列表都是我自己创建的不同对象。
基本上,我需要一种方法来将这些对象从Intent传递给活动。我可以使用addExtras(),但这需要一个Parceable兼容类。我可以使我的类传递序列化,但据我所知,这减慢了程序。
我有什么选择?
我可以传递一个Enum吗?
题外话:有没有一种方法可以将参数从Intent传递给Activity构造函数?
我有一个活动,启动时需要访问两个不同的数组列表。两个列表都是我自己创建的不同对象。
基本上,我需要一种方法来将这些对象从Intent传递给活动。我可以使用addExtras(),但这需要一个Parceable兼容类。我可以使我的类传递序列化,但据我所知,这减慢了程序。
我有什么选择?
我可以传递一个Enum吗?
题外话:有没有一种方法可以将参数从Intent传递给Activity构造函数?
当前回答
不要使用枚举。不使用枚举的第78个理由。:)使用整数,可以通过Bundle和Parcelable轻松远程。
其他回答
关于Oderik的帖子:
你可以让你的枚举实现Parcelable,这对枚举来说非常简单: MyEnum实现Parcelable { ... } 你可以使用Intent。Parcelable putExtra(字符串)。
如果你定义了MyEnum变量MyEnum,那么执行intent。putExtra("Parcelable1", myEnum),你会得到一个"方法putExtra(String, Parcelable)是模棱两可的类型意图"错误消息。 因为还有一个意图。putExtra(String, Parcelable)方法,和原来的'Enum'类型本身实现了Serializable接口,所以编译器不知道选择哪个方法(意图。putExtra(String, Parcelable/or Serializable))。
建议从MyEnum中删除Parcelable接口,并将核心代码移动到wrap类的Parcelable实现中,就像这样(Father2是一个Parcelable,包含一个enum字段):
public class Father2 implements Parcelable {
AnotherEnum mAnotherEnum;
int mField;
public Father2(AnotherEnum myEnum, int field) {
mAnotherEnum = myEnum;
mField = field;
}
private Father2(Parcel in) {
mField = in.readInt();
mAnotherEnum = AnotherEnum.values()[in.readInt()];
}
public static final Parcelable.Creator<Father2> CREATOR = new Parcelable.Creator<Father2>() {
public Father2 createFromParcel(Parcel in) {
return new Father2(in);
}
@Override
public Father2[] newArray(int size) {
return new Father2[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(mField);
dest.writeInt(mAnotherEnum.ordinal());
}
}
然后我们可以做:
AnotherEnum anotherEnum = AnotherEnum.Z;
intent.putExtra("Serializable2", AnotherEnum.X);
intent.putExtra("Parcelable2", new Father2(AnotherEnum.X, 7));
使用Kotlin扩展函数
inline fun <reified T : Enum<T>> Intent.putExtra(enumVal: T, key: String? = T::class.qualifiedName): Intent =
putExtra(key, enumVal.ordinal)
inline fun <reified T: Enum<T>> Intent.getEnumExtra(key: String? = T::class.qualifiedName): T? =
getIntExtra(key, -1)
.takeUnless { it == -1 }
?.let { T::class.java.enumConstants[it] }
这使您可以灵活地传递多个相同的枚举类型,或者默认使用类名。
// Add to gradle
implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
// Import the extension functions
import path.to.my.kotlin.script.putExtra
import path.to.my.kotlin.script.getEnumExtra
// To Send
intent.putExtra(MyEnumClass.VALUE)
// To Receive
val result = intent.getEnumExtra<MyEnumClass>()
我认为最好的办法是将这些列表转换为一些可打包的东西,如字符串(或映射?),以将其传递给活动。然后Activity将不得不将其转换回数组。
实现自定义包装是一个痛苦的脖子,所以我将尽可能避免它。
不要使用枚举。不使用枚举的第78个理由。:)使用整数,可以通过Bundle和Parcelable轻松远程。
如果你真的需要,你可以使用name()和valueOf(String)将一个enum序列化为String,如下所示:
class Example implements Parcelable {
public enum Foo { BAR, BAZ }
public Foo fooValue;
public void writeToParcel(Parcel dest, int flags) {
parcel.writeString(fooValue == null ? null : fooValue.name());
}
public static final Creator<Example> CREATOR = new Creator<Example>() {
public Example createFromParcel(Parcel source) {
Example e = new Example();
String s = source.readString();
if (s != null) e.fooValue = Foo.valueOf(s);
return e;
}
}
}
如果你的枚举有可变的状态,这显然是行不通的(他们不应该,真的)。