Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid custom/compound control: Parcelable state is shared and it shouldn't?
    primarykey
    data
    text
    <p>I have created a custom View / Compound Control in my Android application, which lets users "draw" their signature.</p> <p>The signature is contained in a Canvas, linked to a Bitmap object.</p> <p>I want the signature "image" to be preserved when the orientation is changed. Therefore, I have implemented onSaveInstanceState / onRestoreInstanceState by storing the Bitmap's pixels:</p> <pre><code>@Override public Parcelable onSaveInstanceState() { // Call superclass method, retrieve Parcelable Parcelable superState = super.onSaveInstanceState(); if (pictureBitmap_ != null) { // Retrieve the current pixels array. int totalSize = pictureBitmap_.getWidth() * pictureBitmap_.getHeight(); int pixels[] = new int[totalSize]; pictureBitmap_.getPixels(pixels, 0, pictureBitmap_.getWidth(), 0, 0, pictureBitmap_.getWidth(), pictureBitmap_.getHeight()); // Create the saved state object. SavedState savedState = new SavedState(superState); savedState.pixels = pixels; // Return the populated saved state object. return savedState; } else { // Simply pass original Parcelable along return superState; } } @Override public void onRestoreInstanceState(Parcelable state) { // Is the Parcelable an instance of our custom SavedState? if (!(state instanceof SavedState)) { // No, simply delegate to superclass. super.onRestoreInstanceState(state); return; } // Retrieve custom state object. SavedState savedState = (SavedState) state; // Use superclass to restore original state. super.onRestoreInstanceState(savedState.getSuperState()); // Restore custom state (transfer pixels). this.transferPixels_ = savedState.pixels; // If the picture bitmap is already initialized... if (pictureBitmap_ != null) { // Transfer the pixels right away. transferBitmap(); // Refresh the canvas drawSignature(); } } </code></pre> <p>The integer array of pixels is stored in a SavedState object, which is a custom subclass of BaseSavedState:</p> <pre><code>private static class SavedState extends BaseSavedState { // Members int[] pixels; // Methods SavedState(Parcelable superState) { super(superState); } private SavedState(Parcel in) { super(in); in.readIntArray(pixels); } @Override public void writeToParcel(Parcel out, int flags) { super.writeToParcel(out, flags); out.writeIntArray(pixels); } // required field that makes Parcelables from a Parcel @SuppressWarnings("unused") 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>Now, while this is working well if I only have ONE (1) instance of my custom view in my activity... a problem occurs if I have TWO (2) or more instances: when I flip the orientation, all my controls end up displaying the same signature, which was the last control's signature.</p> <p>In other words, the last set of pixels stored in a SavedState object appears to become the "universal" set of pixels for all my control instances within my Activity.</p> <p>How do I ensure that each control has its own independent saved state? </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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. This table or related slice is empty.
    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