簡體   English   中英

在android中實現Parcelable時,不會調用onRestoreInstanceState

[英]implementing Parcelable in android, onRestoreInstanceState is not getting called

以下是我的課程:

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 )對象,我想在其中實現可包裹性,以便可以在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");
}

嘗試這個...

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

使用我的答案 您需要在writeToParcel方法中寫入所有值。 並創建一個構造函數並執行以下步驟。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM