有没有办法在Activity之间传递不可序列化的参数?

时间:2016-09-27 18:17:41

标签: java android serialization

有没有办法在Android中的Activity之间传递不可序列化的数据? 例如,如果我不能序列化FieldForSomeReasonNotSerializable(如Bitmap,LatLng by Google,TextView),我如何将此类传递给Intent:

class MyClass implements java.io.Serializable{
       String s = "serializable";
       FieldForSomeReasonNotSerializable field=new FieldForSomeReasonNotSerializable();
}

1 个答案:

答案 0 :(得分:2)

您可以实现Parcelable而不是Serializable。从android Studio向导(红色灯泡)将帮助您实现Parcelable界面。只需单击Implements方法,然后在类名中使用相同的Red bulb添加parcelable实现

公共类CategoryDTO实现了Parcelable {

@PrimaryKey
private String id;

/**
 * Category Name
 */
private String name;

/**
 * Category Color
 */
private String color;


public CategoryDTO() {

}

public CategoryDTO(String id, String name, String color) {
    this.name = name;
    this.color = color;
    this.id = id;
}

protected CategoryDTO(Parcel in) {
    id = in.readString();
    color = in.readString();
    name = in.readString();

}

public static final Creator<CategoryDTO> CREATOR = new Creator<CategoryDTO>() {
    @Override
    public CategoryDTO createFromParcel(Parcel in) {
        return new CategoryDTO(in);
    }

    @Override
    public CategoryDTO[] newArray(int size) {
        return new CategoryDTO[size];
    }
};

}