Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You do this by implementing <a href="http://developer.android.com/reference/android/view/View.html#onSaveInstanceState%28%29" rel="noreferrer"><code>View#onSaveInstanceState</code></a> and <a href="http://developer.android.com/reference/android/view/View.html#onRestoreInstanceState%28android.os.Parcelable%29" rel="noreferrer"><code>View#onRestoreInstanceState</code></a> and extending the <a href="http://developer.android.com/reference/android/view/View.BaseSavedState.html" rel="noreferrer"><code>View.BaseSavedState</code></a> class.</p> <pre><code>public class CustomView extends View { private int stateToSave; ... @Override public Parcelable onSaveInstanceState() { //begin boilerplate code that allows parent classes to save state Parcelable superState = super.onSaveInstanceState(); SavedState ss = new SavedState(superState); //end ss.stateToSave = this.stateToSave; return ss; } @Override public void onRestoreInstanceState(Parcelable state) { //begin boilerplate code so parent classes can restore state if(!(state instanceof SavedState)) { super.onRestoreInstanceState(state); return; } SavedState ss = (SavedState)state; super.onRestoreInstanceState(ss.getSuperState()); //end this.stateToSave = ss.stateToSave; } static class SavedState extends BaseSavedState { int stateToSave; SavedState(Parcelable superState) { super(superState); } private SavedState(Parcel in) { super(in); this.stateToSave = in.readInt(); } @Override public void writeToParcel(Parcel out, int flags) { super.writeToParcel(out, flags); out.writeInt(this.stateToSave); } //required field that makes Parcelables from a Parcel public static final Parcelable.Creator&lt;SavedState&gt; CREATOR = new Parcelable.Creator&lt;SavedState&gt;() { public SavedState createFromParcel(Parcel in) { return new SavedState(in); } public SavedState[] newArray(int size) { return new SavedState[size]; } }; } } </code></pre> <p>The work is split between the View and the View's SavedState class. You should do all the work of reading and writing to and from the <code>Parcel</code> in the <code>SavedState</code> class. Then your View class can do the work of extracting the state members and doing the work necessary to get the class back to a valid state.</p> <p>Notes: <code>View#onSavedInstanceState</code> and <code>View#onRestoreInstanceState</code> are called automatically for you if <code>View#getId</code> returns a value >= 0. This happens when you give it an id in xml or call <code>setId</code> manually. Otherwise you have to call <code>View#onSaveInstanceState</code> and write the Parcelable returned to the parcel you get in <code>Activity#onSaveInstanceState</code> to save the state and subsequently read it and pass it to <code>View#onRestoreInstanceState</code> from <code>Activity#onRestoreInstanceState</code>.</p> <p>Another simple example of this is the <a href="http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.0.3_r1/android/widget/CompoundButton.java#CompoundButton.onSaveInstanceState%28%29" rel="noreferrer"><code>CompoundButton</code></a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload