我有一个活动,启动时需要访问两个不同的数组列表。两个列表都是我自己创建的不同对象。

基本上,我需要一种方法来将这些对象从Intent传递给活动。我可以使用addExtras(),但这需要一个Parceable兼容类。我可以使我的类传递序列化,但据我所知,这减慢了程序。

我有什么选择?

我可以传递一个Enum吗?

题外话:有没有一种方法可以将参数从Intent传递给Activity构造函数?


当前回答

我喜欢简单。

The Fred activity has two modes -- HAPPY and SAD. Create a static IntentFactory that creates your Intent for you. Pass it the Mode you want. The IntentFactory uses the name of the Mode class as the name of the extra. The IntentFactory converts the Mode to a String using name() Upon entry into onCreate use this info to convert back to a Mode. You could use ordinal() and Mode.values() as well. I like strings because I can see them in the debugger. public class Fred extends Activity { public static enum Mode { HAPPY, SAD, ; } public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.betting); Intent intent = getIntent(); Mode mode = Mode.valueOf(getIntent().getStringExtra(Mode.class.getName())); Toast.makeText(this, "mode="+mode.toString(), Toast.LENGTH_LONG).show(); } public static Intent IntentFactory(Context context, Mode mode){ Intent intent = new Intent(); intent.setClass(context,Fred.class); intent.putExtra(Mode.class.getName(),mode.name()); return intent; } }

其他回答

使用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>()

我喜欢简单。

The Fred activity has two modes -- HAPPY and SAD. Create a static IntentFactory that creates your Intent for you. Pass it the Mode you want. The IntentFactory uses the name of the Mode class as the name of the extra. The IntentFactory converts the Mode to a String using name() Upon entry into onCreate use this info to convert back to a Mode. You could use ordinal() and Mode.values() as well. I like strings because I can see them in the debugger. public class Fred extends Activity { public static enum Mode { HAPPY, SAD, ; } public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.betting); Intent intent = getIntent(); Mode mode = Mode.valueOf(getIntent().getStringExtra(Mode.class.getName())); Toast.makeText(this, "mode="+mode.toString(), Toast.LENGTH_LONG).show(); } public static Intent IntentFactory(Context context, Mode mode){ Intent intent = new Intent(); intent.setClass(context,Fred.class); intent.putExtra(Mode.class.getName(),mode.name()); return intent; } }

大多数使用Parcelable概念的答案都是在Java代码中。在Kotlin中更容易做到这一点。

只需用@Parcelize注释枚举类并实现Parcelable接口。

@Parcelize
enum class ViewTypes : Parcelable {
TITLE, PRICES, COLORS, SIZES
}

考虑以下enum::

public static  enum MyEnum {
    ValueA,
    ValueB
}

对于通过::

 Intent mainIntent = new Intent(this,MyActivity.class);
 mainIntent.putExtra("ENUM_CONST", MyEnum.ValueA);
 this.startActivity(mainIntent);

从intent/bundle/arguments中返回:

 MyEnum myEnum = (MyEnum) intent.getSerializableExtra("ENUM_CONST");

你可以使用枚举构造函数为enum设置基本数据类型。

public enum DaysOfWeek {
    MONDAY(1),
    TUESDAY(2),
    WEDNESDAY(3),
    THURSDAY(4),
    FRIDAY(5),
    SATURDAY(6),
    SUNDAY(7);

    private int value;
    private DaysOfWeek(int value) {
        this.value = value;
    }

    public int getValue() {
        return this.value;
    }

    private static final SparseArray<DaysOfWeek> map = new SparseArray<DaysOfWeek>();

    static
    {
         for (DaysOfWeek daysOfWeek : DaysOfWeek.values())
              map.put(daysOfWeek.value, daysOfWeek);
    }

    public static DaysOfWeek from(int value) {
        return map.get(value);
    }
}

你可以使用传递int作为额外参数,然后使用它的值从enum中取出它。