如何保存所有字段的状态,我在我的类中扩展了View?在View类中保存所有字段的状态 - Android
@Override
protected Parcelable onSaveInstanceState() {
Parcelable localState = super.onSaveInstanceState();
Bundle bundle = new Bundle();
bundle.putSerializable("gameEngine", this.gameEngine);
bundle.putParcelable("inherited", localState);
return bundle;
}
@Override
protected void onRestoreInstanceState(Parcelable localState) {
Bundle bundle = (Bundle) localState;
this.gameEngine = ((GameEngine) bundle.getSerializable("gameEngine"));
super.onRestoreInstanceState(bundle.getParcelable("inherited"));
}
这似乎并不拯救领域。
谢谢。
这个答案是正确的,但是对于需要存储大量变量的人来说,这可能不是最好的解决方案。 另外我发现你不能把一个多维数组放在包中。我不得不创建一个算法来将多重拼合成一个单独的用于存储,然后将另一个重新组合成多个用于恢复。 谢谢:) – Waddas 2013-05-02 00:25:43
据我所知,这是做到这一点的唯一方法。 您可以将putSerializable用于多维数组。请注意,通过Bundle的所有内容都被序列化(克隆),因此所有对其他活动的引用都将丢失。您可以使用静态变量来保留它们。 对于最近的答案抱歉,我来自欧洲。 – PhpXp 2013-05-02 07:48:16