[英]implementing Parcelable in android, onRestoreInstanceState is not getting called
Following is my class: 以下是我的课程:
public class Line implements Parcelable {
private Point start, end;
public Line() {
// TODO Auto-generated constructor stub
}
public Line(Point start, Point end) {
this.end = end;
this.start = start;
}
public Point getStart() {
return start;
}
public void setStart(Point start) {
this.start = start;
}
public Point getEnd() {
return end;
}
public void setEnd(Point end) {
this.end = end;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
// TODO Auto-generated method stub
}
}
it contains two Point( android.graphics.Point
) objects and i would like to implement parcelable in it, so that I can restore ArrayList of Line
objects in Activity
. 它包含两个Point(
android.graphics.Point
)对象,我想在其中实现可包裹性,以便可以在Activity
恢复Line
对象的ArrayList。
The problem as both my attributes are of type Point not sure how to write it in writeToParcel
and read it in 问题是我的两个属性均为Point类型,不确定如何在
writeToParcel
写入和读取
public Line(Parcel in) {
super();
}
EDIT 编辑
following the answer I implemented the Line class. 按照答案,我实现了Line类。 But in activity the problem is
onRestoreInstanceState
is never getting called. 但是在活动中,问题是永远不会调用
onRestoreInstanceState
。
When I press home button, and get back to the app all the data in my arrayLists is lost. 当我按下主页按钮,并返回到应用程序时,我的arrayLists中的所有数据都丢失了。
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
// Save UI state changes to the savedInstanceState.
// This bundle will be passed to onCreate if the process is
// killed and restarted.
savedInstanceState.putInt("player", player);
savedInstanceState.putParcelableArrayList("lines", lines);
savedInstanceState.putParcelableArrayList("rects1", rects1);
savedInstanceState.putParcelableArrayList("rects2", rects2);
// etc.
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// Restore UI state from the savedInstanceState.
// This bundle has also been passed to onCreate.
player = savedInstanceState.getInt("player");
lines = savedInstanceState.getParcelableArrayList("lines");
rects1 = savedInstanceState.getParcelableArrayList("rects1");
rects2 = savedInstanceState.getParcelableArrayList("rects2");
}
try this... 尝试这个...
public class Line implements Parcelable {
private Point start, end;
public Line() {
}
public Line(Point start, Point end) {
this.end = end;
this.start = start;
}
public Point getStart() {
return start;
}
public void setStart(Point start) {
this.start = start;
}
public Point getEnd() {
return end;
}
public void setEnd(Point end) {
this.end = end;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeParcelable(start, flags);
dest.writeParcelable(end, flags);
}
@Override
public int describeContents() {
return 0xDEAF;
}
public static final Parcelable.Creator<Line> CREATOR = new Parcelable.Creator<Line>() {
@Override
public Line createFromParcel(Parcel source) {
Line line = new Line();
Point start = source.readParcelable(Point.class.getClassLoader());
Point end = source.readParcelable(Point.class.getClassLoader());
line.setStart(start);
line.setEnd(end);
return line;
}
@Override
public Line[] newArray(int size) {
return new Line[size];
}
};
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.