java - 在android中实现Parcelable,onRestoreInstanceState没有被调用

标签 java android parcelable

以下是我的类(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

}
}

它包含两个 Point( android.graphics.Point ) 对象,我想在其中实现 Parcelable,这样我就可以恢复 Line 的 ArrayList Activity 中的对象。

问题是我的两个属性都是 Point 类型,不知道如何在 writeToParcel 中编写它并在

中阅读
public Line(Parcel in) {
        super();

    }

编辑

根据答案,我实现了 Line 类。但在 Activity 中问题是 onRestoreInstanceState永远不会被调用。

当我按主页按钮并返回应用程序时,数组列表中的所有数据都会丢失。

    @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");
}

最佳答案

试试这个...

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];
        }
    };
}

关于java - 在android中实现Parcelable,onRestoreInstanceState没有被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21301989/

相关文章:

Java嵌套parcelable,无法初始化数组

java - 定位推送通知

java - 使用桌面的默认应用程序打开存储在 jar 文件中的 PDF 文件

android - 带有 parcelable 的 EXTRA 结果为 NULL 但没有罚款

android - <select> 框未在 PhoneGap 的 Android 上显示

android - 仅当任务被另一个任务调用时才添加 Gradle 任务依赖

android - 如何查找类型为 java.lang.RuntimeException : Parcel android. os.Parcel@#### 的崩溃源:在偏移量 YYY 处解码未知类型代码 XXXX

java - 从 Java 1.6 切换到 1.7 导致 JAXB 异常

java - 如何在 JDL-Studio 中为 String 指定 MySQL 数据类型

java - 无法找到代码来根据列表项位置替换详细信息 fragment