是否可以将自定义类放入Bundle?

时间:2019-05-17 21:35:19

标签: android class android-intent bundle

在我的android项目中,我将“设置”定义为一个类,其中包含全局设置值,并且我将在许多不同的活动中使用它。因此,我想使用Bundle传递此类,并尝试使用Parcelable,但它不起作用?您能说出最佳做法是什么吗?

这是我的课程:

public class Setting{
        String set1;
        boolean set2;
        int set3;
    }

这就是我试图将意图捆绑到另一个活动的方法:

...
Setting sets = new Setting();
sets.set1 = "test";
sets.set2 = true;
sets.set3 = 1;
...
Bundle b = new Bundle();
b.putParcelable("pass_settings", (Parcelable) sets );

4 个答案:

答案 0 :(得分:3)

您必须实现Parcelable接口:

public class Setting implements Parcelable {
    String set1;
    boolean set2;
    int set3;

    protected Setting(Parcel in) {
        set1 = in.readString();
        set2 = in.readByte() != 0;
        set3 = in.readInt();
    }

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

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

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(set1);
        dest.writeByte((byte) (set2 ? 1 : 0));
        dest.writeInt(set3);
    }
}

然后您可以将其放入捆绑包中:

bundle.putParcelable("tag_here", setting);

答案 1 :(得分:0)

您可以使用gson库将整个设置对象转换为json字符串,方法是将其从当前活动传递到意图,然后在接收活动时将其还原回设置对象。

当前活动:

 1. Gson gson = new Gson();
 2. String json = gson.toJson(sets);

注意:将json字符串传递给intent。

接收活动: 注意:从intent接收json字符串。

 1. Gson gson = new Gson();
 2. Setting sets = gson.fromJson(json, Setting.class);

答案 2 :(得分:0)

您必须实现Paracable

public class Settings implements Paracable{

    String set1;
    boolean set2;
    int set3;

   // Parcelling part

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

        @Override
    public Setting[] newArray(int size) {
        return new Setting[size];
    }
};
   public Settings (Parcel in){
       this.set1= in.readLong();
       this.set2 = in.readString();
       this.set3 =  in.readString();
   }

   @Override
   public int describeContents() {
       return 0;
   }

   @Override
   public void writeToParcel(Parcel dest, int flags) {
      dest.writeString(this.set1);
      dest.writeBoolean(this.set2);
      dest.writeInt(this.set3);
   }
}

答案 3 :(得分:0)

public class Settings implements Parcelable {
    private String mSet1;
    private boolean mSet2;
    private int mSet3;

    public Settings(){}

    protected Settings(Parcel in) {
        mSet1 = in.readString();
        mSet2 = in.readByte() != 0;
        mSet3 = in.readInt();
    }

    public String getSet1() {
        return mSet1;
    }

    public void setSet1(final String set1) {
        mSet1 = set1;
    }

    public boolean isSet2() {
        return mSet2;
    }

    public void setSet2(final boolean set2) {
        mSet2 = set2;
    }

    public int getSet3() {
        return mSet3;
    }

    public void setSet3(final int set3) {
        mSet3 = set3;
    }

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

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

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(final Parcel dest, final int flags) {
        dest.writeString(mSet1);
        dest.writeByte((byte) (mSet2 ? 1 : 0));
        dest.writeInt(mSet3);
    }

    private void test(){
        Settings toSave = new Settings();
        Bundle bundle = new Bundle();
        bundle.putParcelable("bundle_key", toSave);

        Settings settings = bundle.getParcelable("bundle_key`enter code here`"); 
    }
}
相关问题