您可以保存不僅parcelable領域,例如:
public class ViewModel extends BaseObservable implements Parcelable
{
private ObservableField<String> title = new ObservableField<>();
private ObservableInt count = new ObservableInt();
private ViewModel(Parcel parcel)
{
title.set(parcel.readString());
count.set(parcel.readInt());
}
@Override
public int describeContents()
{
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int flags)
{
parcel.writeString(title.get());
parcel.writeInt(count.get());
}
public static final Creator<ViewModel> CREATOR = new Creator<ViewModel>()
{
@Override
public ViewModel createFromParcel(Parcel in)
{
return new ViewModel(in);
}
@Override
public ViewModel[] newArray(int size)
{
return new ViewModel[size];
}
};
}
但是,如果你想與parcelable工作,你應該實現這個接口爲你的類
public class ParcelableString implements Parcelable
{
private String value;
/*Getter, Setter and implementation of Parcelable*/
}
爲什麼你要混合'BaseObservable'和'ObservableField'(和它的兄弟姐妹)? – pskink
我有擴展BaseObservable並具有ObservableField作爲字段的對象。我希望這個對象可以被parcelable,但是observablefields不可以被parcelable –