如何使用onSaveInstanceState在自定义视图上保存对象的二维数组?

时间:2019-04-19 10:26:34

标签: android object parcelable onsaveinstancestate

当我在自定义视图中调用onSaveInstanceState时,我试图使用parcelable来保存对象的二维数组,但是我很难弄清它们的工作方式。我应该如何实施这些方法?这是正确的方法吗? 对于每个单元格,我需要保存哪一侧是真或假,哪一侧已被触摸以及该单元对象上的其他信息

public class GameMap extends View {

private Cell[][] cells;
/*****/

@Override
protected Parcelable onSaveInstanceState() {
    Bundle bundle = new Bundle();
    bundle.putParcelable("superState", super.onSaveInstanceState());
    //here need to save cells
    return bundle;
}

@Override
protected void onRestoreInstanceState(Parcelable state) {
    if(state instanceof Bundle) {
        Bundle bundle = (Bundle) state;
        //need to get cells
        state = bundle.getParcelable("superState");
    }
    super.onRestoreInstanceState(state);
}
/****/
private class Cell implements Parcelable{
    private boolean
        topWall = false,
        bottomWall = false,
        leftWall = false,
        rightWall = false;
    private  int column, row;
    private boolean clickable = false;
    private boolean closed = false;
    private int lastAdded = 0;
    private int closedBy = 100;
    private float startx, endx, starty, endy =0;

    public Cell(int column, int row){
        this.column=column;
        this.row=row;
    }
    private void setFloatVar( float startx, float starty, float endy, float endx){
        this.startx=startx;
        this.starty=starty;
        this.endx=endx;
        this.endy=endy;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
            //to implement
    }
    public final Parcelable.Creator<Cell> CREATOR
            = new Parcelable.Creator<Cell>(){
        //to implement
    };

    private Cell(Parcel in){
        //to implment
    }
}

0 个答案:

没有答案