是否可以将自定义 class 放入 Bundle 中?
Is it possible put a custom class into Bundle?
在我的 android 项目中,我将 class 定义为 'Settings',其中包括全局设置值,我将在许多不同的活动中使用它。所以我想通过使用 Bundle 传递这个 class 并且我尝试作为 Parcelable 但它不起作用?你能说出最佳做法是什么吗?
这是我的 class:
public class Setting{
String set1;
boolean set2;
int set3;
}
这就是我尝试将意图捆绑到另一个 activity:
...
Setting sets = new Setting();
sets.set1 = "test";
sets.set2 = true;
sets.set3 = 1;
...
Bundle b = new Bundle();
b.putParcelable("pass_settings", (Parcelable) sets );
您可以使用 gson 库将整个设置对象转换为 json 字符串,方法是将其从当前 activity 传递给意图,然后在接收到 activity.
当前 Activity:
1. Gson gson = new Gson();
2. String json = gson.toJson(sets);
注意:将 json 字符串传递给 intent。
接收Activity:
注意:从 intent.
接收 json 字符串
1. Gson gson = new Gson();
2. Setting sets = gson.fromJson(json, Setting.class);
您必须实现 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);
您必须实施 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);
}
}
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`");
}
}
在我的 android 项目中,我将 class 定义为 'Settings',其中包括全局设置值,我将在许多不同的活动中使用它。所以我想通过使用 Bundle 传递这个 class 并且我尝试作为 Parcelable 但它不起作用?你能说出最佳做法是什么吗?
这是我的 class:
public class Setting{
String set1;
boolean set2;
int set3;
}
这就是我尝试将意图捆绑到另一个 activity:
...
Setting sets = new Setting();
sets.set1 = "test";
sets.set2 = true;
sets.set3 = 1;
...
Bundle b = new Bundle();
b.putParcelable("pass_settings", (Parcelable) sets );
您可以使用 gson 库将整个设置对象转换为 json 字符串,方法是将其从当前 activity 传递给意图,然后在接收到 activity.
当前 Activity:
1. Gson gson = new Gson();
2. String json = gson.toJson(sets);
注意:将 json 字符串传递给 intent。
接收Activity: 注意:从 intent.
接收 json 字符串 1. Gson gson = new Gson();
2. Setting sets = gson.fromJson(json, Setting.class);
您必须实现 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);
您必须实施 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);
}
}
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`");
}
}