Skip to content

Instantly share code, notes, and snippets.

@matthew-carroll
Last active July 6, 2019 11:28
Show Gist options
  • Save matthew-carroll/8cbb5bd52879abac00706069c738152a to your computer and use it in GitHub Desktop.
Save matthew-carroll/8cbb5bd52879abac00706069c738152a to your computer and use it in GitHub Desktop.
Implements onRestoreInstanceState in a custom View in Android.
@Override
public void onRestoreInstanceState(Parcelable state) {
// Cast the incoming Parcelable to our custom SavedState. We produced
// this Parcelable before, so we know what type it is.
SavedState savedState = (SavedState) state;
// Let our super class process state before we do because we should
// depend on our super class, we shouldn't imply that our super class
// might need to depend on us.
super.onRestoreInstanceState(savedState.getSuperState());
// Grab our properties out of our SavedState.
this.name = savedState.name;
this.index = savedState.index;
// Update our visuals in whatever way we want, like...
requestLayout(); //...or...
invalidate(); //...or...
doSomethingCustom();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment