0

我有一个扩展 FrameLayout 的自定义 ViewGroup。在方向更改时,我需要保留一个 int 值。我正在覆盖onSaveInstanceState()and onRestoreInstanceState(Parcelable state)以保持旧值。我的代码如下

                @Override
                    protected Parcelable onSaveInstanceState() {
                        final Bundle bundle = new Bundle();
                        bundle.putInt(POSITION, position);
                        return bundle;
                    }

                    @Override
                    protected void onRestoreInstanceState(Parcelable state) {
                        if (state instanceof Bundle) {
                            Bundle bundle = (Bundle) state;
                            mPosition = bundle.getInt(POSITION);
                        }
                        super.onRestoreInstanceState(state);
                    }

但我要崩溃了。

java.lang.IllegalStateException: Derived class did not call super.onSaveInstanceState()

但是,如果我打电话super.onSaveInstanceState()onSaveInstanceState我如何返回我的捆绑价值?

这里有什么问题?

提前致谢!!

4

1 回答 1

0

调用超级方法 onSaveInstanceState()

 @Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);  // call super method
    // your code
}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    // your code
}
于 2017-02-27T06:44:32.807 回答