简体   繁体   English

在android中实现Parcelable时,不会调用onRestoreInstanceState

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

Use My Answer . 使用我的答案 you need to write all the values in writeToParcel method. 您需要在writeToParcel方法中写入所有值。 and create one Constructor and perform below steps. 并创建一个构造函数并执行以下步骤。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 调用了onSaveInstanceState和onRestoreInstanceState,但是在轮换数据上已重置 - onSaveInstanceState and onRestoreInstanceState being called but yet on rotation data is getting reset Android:从不调用Parcelable.writeToParcel和Parcelable.Creator.createFromParcel - Android: Parcelable.writeToParcel and Parcelable.Creator.createFromParcel are never called 在DataBaseHelper类中实现Parcelable - Implementing Parcelable in DataBaseHelper class Android从onRestart访问onRestoreInstanceState - Android access onRestoreInstanceState from onRestart 为日期对象实现可打包-NullPointerException - Implementing Parcelable for Date Object - NullPointerException Android 的 Parcelable 接口如何强制实现类具有 static 字段? - How does Android's Parcelable interface force implementing classes to have a static field? 如何摆脱为每个类在Android中实现Parcelable以便将其用于放置额外的数组列表 - How to get rid of implementing Parcelable for each class in android in order to use it for put extra array list 在Android中未调用方法 - Method not getting called in android 警报没有在Android中被调用 - Alarm not getting called in android Android-只能在onRestoreInstanceState()中还原saveInstanceState,而不能在onCreate()中还原 - Android - savedInstanceState can be restored only in onRestoreInstanceState() not in onCreate()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM