Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BadParcelableException ClassNotFoundException CoordinatorLayout$SavedState

I have a custom view extending CoordinatorLayout and I am overriding onRestoreInstanceState and onSaveInstanceState the typical way using a class extending BaseSavedState. When my views get restored i get:


Caused by: android.os.BadParcelableException: ClassNotFoundException when unmarshalling: android.support.design.widget.CoordinatorLayout$SavedState
at android.os.Parcel.readParcelableCreator(Parcel.java:2432)
at android.os.Parcel.readParcelable(Parcel.java:2358)
at android.view.AbsSavedState.(AbsSavedState.java:57)
at android.view.View$BaseSavedState.(View.java:21457)
at android.support.design.widget.CoordinatorLayout$SavedState.(CoordinatorLayout.java:2656)

If I comment my two methods onRestoreInstanceState and onSaveInstanceState this issue is solved. See custom view code below:

 @Override
  protected Parcelable onSaveInstanceState() {
    Parcelable superState = super.onSaveInstanceState();
    SavedState ss = new SavedState(superState);
    ss.scrollX = scrollView.getScrollX();
    ss.scrollY = scrollView.getScrollY();
    return ss;
  }

  @Override
  protected void onRestoreInstanceState(Parcelable state) {
    if (!(state instanceof SavedState)) {
      super.onRestoreInstanceState(state);
      return;
    }
    final SavedState ss = (SavedState) state;
    super.onRestoreInstanceState(ss.getSuperState());

    post(() -> scrollView.scrollTo(ss.scrollX, ss.scrollY));
  }

  static class SavedState extends BaseSavedState {
    int scrollX;
    int scrollY;

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

    private SavedState(Parcel in) {
      super(in);
      this.scrollX = in.readInt();
      this.scrollY = in.readInt();
    }

    @Override
    public void writeToParcel(Parcel out, int flags) {
      super.writeToParcel(out, flags);
      out.writeInt(this.scrollX);
      out.writeInt(this.scrollY);
    }

    //required field that makes Parcelables from a Parcel
    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];
          }
        };
  }
like image 262
Niqo Avatar asked Jan 06 '23 11:01

Niqo


1 Answers

It's because the AbsSavesState uses a null class loader so you have to serialize it yourself:


@Override
  protected Parcelable onSaveInstanceState() {
    Parcelable superState = super.onSaveInstanceState();
    SavedState ss = new SavedState(superState);
    ss.scrollX = scrollView.getScrollX();
    ss.scrollY = scrollView.getScrollY();
    return ss;
  }

  @Override
  protected void onRestoreInstanceState(Parcelable state) {
    if (!(state instanceof SavedState)) {
      super.onRestoreInstanceState(state);
      return;
    }
    final SavedState ss = (SavedState) state;
    super.onRestoreInstanceState(ss.superState);

    post(() -> scrollView.scrollTo(ss.scrollX, ss.scrollY));
  }

  static class SavedState extends BaseSavedState {
    Parcelable superState;
    int scrollX;
    int scrollY;

    SavedState(Parcelable superState) {
      // We store the state ourself and send an empty state to to the parent
      super(EMPTY_STATE);
      this.superState = superState;
    }

    private SavedState(Parcel in) {
      super(in);
      this.superState = in.readParcelable(CoordinatorLayout.SavedState.class.getClassLoader());
      this.scrollX = in.readInt();
      this.scrollY = in.readInt();
    }

    @Override
    public void writeToParcel(Parcel out, int flags) {
      super.writeToParcel(out, flags);
      out.writeParcelable(superState, flags);
      out.writeInt(this.scrollX);
      out.writeInt(this.scrollY);
    }

    //required field that makes Parcelables from a Parcel
    public static final Parcelable.Creator CREATOR =
        new Parcelable.Creator() {
          public SavedState createFromParcel(Parcel in) {
            return new SavedState(in);
          }

          public SavedState[] newArray(int size) {
            return new SavedState[size];
          }
        };
  }
like image 184
Nicklas A. Avatar answered Jan 17 '23 15:01

Nicklas A.