FrameLayout中的onSaveInstanceState导致崩溃

时间:2017-02-27 06:40:18

标签: android android-framelayout android-savedstate onrestoreinstancestate

我有一个自定义ViewGroup扩展FrameLayout。在方向改变时,我需要保持一个int值。我覆盖了onSaveInstanceState()和。{ 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,我该如何归还我的套餐值?

这里有什么问题?

提前致谢!!

1 个答案:

答案 0 :(得分: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
}