Passing enum or object through an intent (the best solution)

前端 未结 15 832
时光取名叫无心
时光取名叫无心 2020-12-04 05:41

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

Basically I need a way to pa

相关标签:
15条回答
  • 2020-12-04 06:24

    Most of the answers that are using Parcelable concept here are in Java code. It is easier to do it in Kotlin.

    Just annotate your enum class with @Parcelize and implement Parcelable interface.

    @Parcelize
    enum class ViewTypes : Parcelable {
    TITLE, PRICES, COLORS, SIZES
    }
    
    0 讨论(0)
  • 2020-12-04 06:27

    about Oderik's post:

    You can make your enum implement Parcelable which is quite easy for enums:

    public enum MyEnum implements Parcelable { ... } You can than use Intent.putExtra(String, Parcelable).

    If you define a MyEnum variable myEnum, then do intent.putExtra("Parcelable1", myEnum), you will get a "The method putExtra(String, Parcelable) is ambiguous for the type Intent" error message. because there is also a Intent.putExtra(String, Parcelable) method, and original 'Enum' type itself implements the Serializable interface, so compiler does not know choose which method(intent.putExtra(String, Parcelable/or Serializable)).

    Suggest that remove the Parcelable interface from MyEnum, and move the core code into wrap class' Parcelable implementation, like this(Father2 is a Parcelable and contain an enum field):

    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());
    }
    
    }
    

    then we can do:

    AnotherEnum anotherEnum = AnotherEnum.Z;
    intent.putExtra("Serializable2", AnotherEnum.X);   
    intent.putExtra("Parcelable2", new Father2(AnotherEnum.X, 7));
    
    0 讨论(0)
  • 2020-12-04 06:27

    I like simple.

    • 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;
          }
      }
      
    0 讨论(0)
提交回复
热议问题