【问题标题】:implementing Parcelable in android, onRestoreInstanceState is not getting called在android中实现Parcelable,onRestoreInstanceState没有被调用
【发布时间】:2014-01-23 16:30:30
【问题描述】:

以下是我的课:

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,以便我可以在 Activity 中恢复 Line 对象的 ArrayList。

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

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

    }

编辑

按照答案,我实现了 Line 类。但在活动中,问题是 onRestoreInstanceState 永远不会被调用。

当我按下主页按钮并返回应用程序时,我的 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");
}

【问题讨论】:

  • 那有什么问题呢?你能解释一下吗?
  • Point 已经实现了 Parcelable 接口,所以你可以在点上调用WriteToParcel()。只需确保按照将它们写入包裹的顺序从包裹中读取它们即可。
  • 我不知道如何读写这些对象到 Parcel。

标签: java android parcelable


【解决方案1】:

试试这个...

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

【讨论】:

  • 您不需要将Point 类的类加载器传递给source.readparcelable() 函数吗?当您读出 Point,而不是 Line
  • @ThaMe90 在这里我们甚至可以通过null。请参阅文档A ClassLoader from which to instantiate the Parcelable object, or null for the default class loader
  • @Gopal Line 类没有显示任何错误,我想也可以保存,但现在的问题是在 reStore 上没有接到电话,请再次查看我的问题。
  • @vipulmittal onRestoreInstanceState() 仅在您更改方向时才会被调用。当你按下主页按钮时它不会被调用
  • 所以如何保存我的数组列表,因为当我按 home 时返回我的应用程序列表是空的
【解决方案2】:

使用我的Answer。 您需要在 writeToParcel 方法中写入所有值。 并创建一个构造函数并执行以下步骤。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-22
    • 2012-07-14
    相关资源
    最近更新 更多