Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exception in onRestoreInstanceState #17

Open
bloodyraoul opened this issue Oct 19, 2016 · 2 comments
Open

Exception in onRestoreInstanceState #17

bloodyraoul opened this issue Oct 19, 2016 · 2 comments

Comments

@bloodyraoul
Copy link

bloodyraoul commented Oct 19, 2016

On the following code extracted from SmoothCheckBox.java, you use the same key for Parcelable and Boolean values, so a cast exception occurs when activity is restored.
You should use KEY_INSTANCE_STATE for one and another key for the other.

@Override
    protected Parcelable onSaveInstanceState() {
        Bundle bundle = new Bundle();
        bundle.putParcelable(KEY_INSTANCE_STATE, super.onSaveInstanceState());
        bundle.putBoolean(KEY_INSTANCE_STATE, isChecked());
        return bundle;
    }

    @Override
    protected void onRestoreInstanceState(Parcelable state) {
        if (state instanceof Bundle) {
            Bundle bundle = (Bundle) state;
            boolean isChecked = bundle.getBoolean(KEY_INSTANCE_STATE);
            setChecked(isChecked);
            super.onRestoreInstanceState(bundle.getParcelable(KEY_INSTANCE_STATE));
            return;
        }
        super.onRestoreInstanceState(state);
    }
@ryuunami
Copy link

也出现了这个问题,怎么修改,getBoolean和getParcelable用不同的key倒是不会报错,但勾选状态依然保存不住。

@ukzzang
Copy link

ukzzang commented Apr 25, 2020

private static final String KEY_INSTANCE_STATE = "InstanceState";
private static final String KEY_CHECKED_STATE = "CheckedState";
...
bundle.putParcelable(KEY_INSTANCE_STATE, super.onSaveInstanceState());
bundle.putBoolean(KEY_CHECKED_STATE, isChecked());
...
boolean isChecked = bundle.getBoolean(KEY_CHECKED_STATE);
setChecked(isChecked);
super.onRestoreInstanceState(bundle.getParcelable(KEY_INSTANCE_STATE));

This code can be fixed this exception.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants