通过意图传递枚举或对象(最佳解决方案) [英] Passing enum or object through an intent (the best solution)

查看:169
本文介绍了通过意图传递枚举或对象(最佳解决方案)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,当开始需要访问两个不同的ArrayList的活动。这两个列表都是我创造自己不同的对象。

I have an activity that when started needs access to two different ArrayLists. Both Lists are different Objects I have created myself.

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

Basically I need a way to pass these objects to the activity from an Intent. I can use addExtras() but this requires a Parceable compatible class. I could make my classes to be passed serializable but as I understand this slows down the program.

我有什么选择?

我可以通过一个枚举?

作为一个顺便说一句:有没有办法从意图传递参数的活动构造

As an an aside: is there a way to pass parameters to an Activity Constructor from an Intent?

推荐答案

这是一个老问题,但大家都没有提到枚举实际上是序列化,因此可以完美地添加到意向作为一个额外的。像这样的:

This is an old question but everybody fails to mention that Enums are actually Serializable and therefore can perfectly be added to an Intent as an extra. Like this:

public enum AwesomeEnum {
  Something, Other;
};

intent.putExtra("AwesomeEnum", AwesomeEnum.Something);

AwesomeEnum result = (AwesomeEnum) intent.getSerializableExtra("AwesomeEnum");

要使用静态或应用程序范围的变量的建议是一个非常糟糕的主意。这真的将你的活动状态管理系统,这是很难维护,调试和问题的约束。

The suggestion to use static or application wide variables is a really bad idea. This really couples your activities to a state managing system and it is hard to maintain, debug and problem bound.

选择:

一个很好的点所指出的 tedzyc 有关,该解决方案通过的Oderik 给你一个错误。然而,所提供的另一种方法是有点弄碎,一些使用(甚至使用泛型)。

A good point was noted by tedzyc about the fact that the solution provided by Oderik gives you an error. However, the alternative provided is a bit crumble-some to use (even using generics).

如果你实在担心添加枚举意图的表现,我建议这些替代品来代替:

If you are really worried about the performance of adding the enum to an intent I propose these alternatives instead:

选项1:

public enum AwesomeEnum {
  Something, Other;
  private static final String name = AwesomeEnum.class.getName();
  public void attachTo(Intent intent) {
    intent.putExtra(name, ordinal());
  }
  public static AwesomeEnum detachFrom(Intent intent) {
    if(!intent.hasExtra(name)) throw new IllegalStateException();
    return values()[intent.getIntExtra(name, -1)];
  }
}

用法:

// Sender usage
AwesomeEnum.Something.attachTo(intent);
// Receiver usage
AwesomeEnum result = AwesomeEnum.detachFrom(intent);

选项2:的 (通用的,可重复使用和枚举去耦)

OPTION 2: (generic, reusable and decoupled from the enum)

public final class EnumUtil {
    public static class Serializer<T extends Enum<T>> extends Deserializer<T> {
        private T victim;
        @SuppressWarnings("unchecked") 
        public Serializer(T victim) {
            super((Class<T>) victim.getClass());
            this.victim = victim;
        }
        public void to(Intent intent) {
            intent.putExtra(name, victim.ordinal());
        }
    }
    public static class Deserializer<T extends Enum<T>> {
        protected Class<T> victimType;
        protected String name;
        public Deserializer(Class<T> victimType) {
            this.victimType = victimType;
            this.name = victimType.getName();
        }
        public T from(Intent intent) {
            if (!intent.hasExtra(name)) throw new IllegalStateException();
            return victimType.getEnumConstants()[intent.getIntExtra(name, -1)];
        }
    }
    public static <T extends Enum<T>> Deserializer<T> deserialize(Class<T> victim) {
        return new Deserializer<T>(victim);
    }
    public static <T extends Enum<T>> Serializer<T> serialize(T victim) {
        return new Serializer<T>(victim);
    }
}

用法:

// Sender usage
EnumUtil.serialize(AwesomeEnum.Something).to(intent);
// Receiver usage
AwesomeEnum result = EnumUtil.deserialize(AwesomeEnum.class).from(intent);

这篇关于通过意图传递枚举或对象(最佳解决方案)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆