Skip to content

Commit

Permalink
at least with O, we no longer can rely on superstate being empty. Imp…
Browse files Browse the repository at this point in the history
…lement proper parceling of both super and wrapped state
  • Loading branch information
mtotschnig committed Jun 9, 2017
1 parent 4182d8f commit c9f9a44
Showing 1 changed file with 42 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.util.Log;
Expand Down Expand Up @@ -1121,17 +1122,14 @@ public void setMultiChoiceModeListener(MultiChoiceModeListener listener) {

@Override
public Parcelable onSaveInstanceState() {
Parcelable superState = super.onSaveInstanceState();
if (superState != BaseSavedState.EMPTY_STATE) {
throw new IllegalStateException("Handling non empty state of parent class is not implemented");
}
return mList.onSaveInstanceState();
return new SavedState(super.onSaveInstanceState(), mList.onSaveInstanceState());
}

@Override
public void onRestoreInstanceState(Parcelable state) {
super.onRestoreInstanceState(BaseSavedState.EMPTY_STATE);
mList.onRestoreInstanceState(state);
SavedState ss = (SavedState) state;
super.onRestoreInstanceState(ss.getSuperState());
mList.onRestoreInstanceState(ss.wrappedState);
}

@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
Expand All @@ -1155,4 +1153,41 @@ public void setStackFromBottom(boolean stackFromBottom) {
public boolean isStackFromBottom() {
return mList.isStackFromBottom();
}

static class SavedState extends BaseSavedState {
private Parcelable wrappedState;

/**
* Constructor called from {@link StickyListHeadersListView#onSaveInstanceState()}
*/
SavedState(Parcelable superState, Parcelable wrappedState) {
super(superState);
this.wrappedState = wrappedState;
}

/**
* Constructor called from {@link #CREATOR}
*/
private SavedState(Parcel in) {
super(in);
wrappedState = in.readParcelable(null);
}

@Override
public void writeToParcel(Parcel out, int flags) {
super.writeToParcel(out, flags);
out.writeValue(wrappedState);
}

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];
}
};
}
}

0 comments on commit c9f9a44

Please sign in to comment.