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

RatingView does not save state on view recreation #3

Closed
egidijusk opened this issue Aug 30, 2016 · 1 comment
Closed

RatingView does not save state on view recreation #3

egidijusk opened this issue Aug 30, 2016 · 1 comment

Comments

@egidijusk
Copy link

First of all thanks for this great widget because default RatingBar widget really sucks. This one is a good replacement. There's one issue, though. If you change rating and rotate the screen then RatingView is recreated but previously set rating is not reset. It resets to default. I have code that solves this problem. Consider adding it to the RatingView class.

@Override
protected Parcelable onSaveInstanceState() {
    Parcelable superState = super.onSaveInstanceState();
    SavedState savedState = new SavedState(superState);
    savedState.mRating = mRating;
    return savedState;
}

@Override
protected void onRestoreInstanceState(Parcelable state) {
    if (state instanceof SavedState) {
        SavedState savedState = (SavedState) state;
        super.onRestoreInstanceState(savedState.getSuperState());
        mRating = savedState.mRating;
    } else {
        super.onRestoreInstanceState(state);
    }
}

static class SavedState extends BaseSavedState {

    float mRating;

    SavedState(Parcelable superState) {
        super(superState);
    }

    private SavedState(Parcel in) {
        super(in);
        this.mRating = in.readFloat();
    }

    @Override
    public void writeToParcel(Parcel out, int flags) {
        super.writeToParcel(out, flags);
        out.writeFloat(this.mRating);
    }

    public static final Parcelable.Creator<SavedState> CREATOR = new Parcelable.Creator<SavedState>() {
                public SavedState createFromParcel(Parcel in) {
                    return new SavedState(in);
                }

                public SavedState[] newArray(int size) {
                    return new SavedState[size];
                }
            };
}
@Ornolfr
Copy link
Owner

Ornolfr commented Aug 30, 2016

@egidijusk Thank you for your contribution, your code works well. I added saving mIsIndicator value as well. That's needed improvement.

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

No branches or pull requests

2 participants