我的一个活动中有一个文件,我写信给它(类似日志文件)。我想将它传递给另一个活动,并附加一些其他信息。我该怎么做? 我听说过Parcelable对象,但我不知道这是否是正确的解决方案。
答案 0 :(得分:1)
在Appicaltion Class中存储变量是NOT a good OOP concept。通常由您已经提到的Parcelable完成,这是实现它的模型类的一个示例:
public class NumberEntry implements Parcelable {
private int key;
private int timesOccured;
private double appearRate;
private double forecastValue;
public NumberEntry() {
key = 0;
timesOccured = 0;
appearRate = 0;
forecastValue = 0;
}
public static final Parcelable.Creator<NumberEntry> CREATOR = new Parcelable.Creator<NumberEntry>() {
public NumberEntry createFromParcel(Parcel in) {
return new NumberEntry(in);
}
public NumberEntry[] newArray(int size) {
return new NumberEntry[size];
}
};
/**
* private constructor called by Parcelable interface.
*/
private NumberEntry(Parcel in) {
this.key = in.readInt();
this.timesOccured = in.readInt();
this.appearRate = in.readDouble();
this.forecastValue = in.readDouble();
}
/**
* Pointless method. Really.
*/
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(this.key);
dest.writeInt(this.timesOccured);
dest.writeDouble(this.appearRate);
dest.writeDouble(this.forecastValue);
}
但是,像其他人所说的Parcelable本身就是bad design,所以如果你没有遇到性能问题,那么实现Serializable也是另一种选择。