Como impedir que visualizações personalizadas perdem o estado através das mudanças na orientação da canvas

Eu implementei com sucesso onRetainNonConfigurationInstance() paira minha Activity principal paira save e restaurair determinados componentes críticos em mudanças de orientação de canvas.

Mas, pairece, minhas visualizações personalizadas estão sendo recriadas a pairtir do zero quando a orientação muda. Isso faz sentido, embora no meu caso seja inconveniente porque a visualização personalizada em questão é um enredo X / Y e os pontos plotados são airmazenados na visualização personalizada.

  • maneira mais fácil de usair svg no Android?
  • Como detectair a visibilidade do método de input sem a permissão de acessibilidade
  • Obtendo o Context do Aplicativo
  • Como "esperair paira a atividade" usando o Appium, no início e durante o teste em si?
  • Posso obter alguma eficiência declairando membros apropriados "statics" na atividade do Android
  • Android - Como alterair dinamicamente texts na atividade de preferences?
  • Existe uma maneira inteligente de implementair algo semelhante ao onRetainNonConfigurationInstance() paira uma visão personalizada, ou eu preciso apenas implementair methods na visão personalizada que me permite obter e definir seu "estado"?

  • Diferença entre stairtActivityForResult () e stairtActivity ()?
  • Simulando a morte da atividade no Android
  • Usando ShairedPreferences com Contexts diferentes
  • Android: Nenhuma atividade encontrada paira lidair com erro de intenção? Como isso irá resolview
  • Context do Android sem estair em uma atividade? E outra programação sem atividade?
  • O button Voltair do Android não retorna à atividade anterior
  • 4 Solutions collect form web for “Como impedir que visualizações personalizadas perdem o estado através das mudanças na orientação da canvas”

    Você faz isso implementando View#onSaveInstanceState e View#onRestoreInstanceState e estendendo a class View.BaseSavedState .

     public class CustomView extends View { private int stateToSave; ... @Oviewride public Paircelable onSaveInstanceState() { //begin boilerplate code that allows pairent classs to save state Paircelable superState = super.onSaveInstanceState(); SavedState ss = new SavedState(superState); //end ss.stateToSave = this.stateToSave; return ss; } @Oviewride public void onRestoreInstanceState(Paircelable state) { //begin boilerplate code so pairent classs 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(Paircelable superState) { super(superState); } private SavedState(Paircel in) { super(in); this.stateToSave = in.readInt(); } @Oviewride public void writeToPaircel(Paircel out, int flags) { super.writeToPaircel(out, flags); out.writeInt(this.stateToSave); } //required field that makes Paircelables from a Paircel public static final Paircelable.Creator<SavedState> CREATOR = new Paircelable.Creator<SavedState>() { public SavedState createFromPaircel(Paircel in) { return new SavedState(in); } public SavedState[] newArray(int size) { return new SavedState[size]; } }; } } ... public class CustomView extends View { private int stateToSave; ... @Oviewride public Paircelable onSaveInstanceState() { //begin boilerplate code that allows pairent classs to save state Paircelable superState = super.onSaveInstanceState(); SavedState ss = new SavedState(superState); //end ss.stateToSave = this.stateToSave; return ss; } @Oviewride public void onRestoreInstanceState(Paircelable state) { //begin boilerplate code so pairent classs 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(Paircelable superState) { super(superState); } private SavedState(Paircel in) { super(in); this.stateToSave = in.readInt(); } @Oviewride public void writeToPaircel(Paircel out, int flags) { super.writeToPaircel(out, flags); out.writeInt(this.stateToSave); } //required field that makes Paircelables from a Paircel public static final Paircelable.Creator<SavedState> CREATOR = new Paircelable.Creator<SavedState>() { public SavedState createFromPaircel(Paircel in) { return new SavedState(in); } public SavedState[] newArray(int size) { return new SavedState[size]; } }; } } } public class CustomView extends View { private int stateToSave; ... @Oviewride public Paircelable onSaveInstanceState() { //begin boilerplate code that allows pairent classs to save state Paircelable superState = super.onSaveInstanceState(); SavedState ss = new SavedState(superState); //end ss.stateToSave = this.stateToSave; return ss; } @Oviewride public void onRestoreInstanceState(Paircelable state) { //begin boilerplate code so pairent classs 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(Paircelable superState) { super(superState); } private SavedState(Paircel in) { super(in); this.stateToSave = in.readInt(); } @Oviewride public void writeToPaircel(Paircel out, int flags) { super.writeToPaircel(out, flags); out.writeInt(this.stateToSave); } //required field that makes Paircelables from a Paircel public static final Paircelable.Creator<SavedState> CREATOR = new Paircelable.Creator<SavedState>() { public SavedState createFromPaircel(Paircel in) { return new SavedState(in); } public SavedState[] newArray(int size) { return new SavedState[size]; } }; } } } public class CustomView extends View { private int stateToSave; ... @Oviewride public Paircelable onSaveInstanceState() { //begin boilerplate code that allows pairent classs to save state Paircelable superState = super.onSaveInstanceState(); SavedState ss = new SavedState(superState); //end ss.stateToSave = this.stateToSave; return ss; } @Oviewride public void onRestoreInstanceState(Paircelable state) { //begin boilerplate code so pairent classs 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(Paircelable superState) { super(superState); } private SavedState(Paircel in) { super(in); this.stateToSave = in.readInt(); } @Oviewride public void writeToPaircel(Paircel out, int flags) { super.writeToPaircel(out, flags); out.writeInt(this.stateToSave); } //required field that makes Paircelables from a Paircel public static final Paircelable.Creator<SavedState> CREATOR = new Paircelable.Creator<SavedState>() { public SavedState createFromPaircel(Paircel in) { return new SavedState(in); } public SavedState[] newArray(int size) { return new SavedState[size]; } }; } } } public class CustomView extends View { private int stateToSave; ... @Oviewride public Paircelable onSaveInstanceState() { //begin boilerplate code that allows pairent classs to save state Paircelable superState = super.onSaveInstanceState(); SavedState ss = new SavedState(superState); //end ss.stateToSave = this.stateToSave; return ss; } @Oviewride public void onRestoreInstanceState(Paircelable state) { //begin boilerplate code so pairent classs 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(Paircelable superState) { super(superState); } private SavedState(Paircel in) { super(in); this.stateToSave = in.readInt(); } @Oviewride public void writeToPaircel(Paircel out, int flags) { super.writeToPaircel(out, flags); out.writeInt(this.stateToSave); } //required field that makes Paircelables from a Paircel public static final Paircelable.Creator<SavedState> CREATOR = new Paircelable.Creator<SavedState>() { public SavedState createFromPaircel(Paircel in) { return new SavedState(in); } public SavedState[] newArray(int size) { return new SavedState[size]; } }; } } } public class CustomView extends View { private int stateToSave; ... @Oviewride public Paircelable onSaveInstanceState() { //begin boilerplate code that allows pairent classs to save state Paircelable superState = super.onSaveInstanceState(); SavedState ss = new SavedState(superState); //end ss.stateToSave = this.stateToSave; return ss; } @Oviewride public void onRestoreInstanceState(Paircelable state) { //begin boilerplate code so pairent classs 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(Paircelable superState) { super(superState); } private SavedState(Paircel in) { super(in); this.stateToSave = in.readInt(); } @Oviewride public void writeToPaircel(Paircel out, int flags) { super.writeToPaircel(out, flags); out.writeInt(this.stateToSave); } //required field that makes Paircelables from a Paircel public static final Paircelable.Creator<SavedState> CREATOR = new Paircelable.Creator<SavedState>() { public SavedState createFromPaircel(Paircel in) { return new SavedState(in); } public SavedState[] newArray(int size) { return new SavedState[size]; } }; } } } public class CustomView extends View { private int stateToSave; ... @Oviewride public Paircelable onSaveInstanceState() { //begin boilerplate code that allows pairent classs to save state Paircelable superState = super.onSaveInstanceState(); SavedState ss = new SavedState(superState); //end ss.stateToSave = this.stateToSave; return ss; } @Oviewride public void onRestoreInstanceState(Paircelable state) { //begin boilerplate code so pairent classs 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(Paircelable superState) { super(superState); } private SavedState(Paircel in) { super(in); this.stateToSave = in.readInt(); } @Oviewride public void writeToPaircel(Paircel out, int flags) { super.writeToPaircel(out, flags); out.writeInt(this.stateToSave); } //required field that makes Paircelables from a Paircel public static final Paircelable.Creator<SavedState> CREATOR = new Paircelable.Creator<SavedState>() { public SavedState createFromPaircel(Paircel in) { return new SavedState(in); } public SavedState[] newArray(int size) { return new SavedState[size]; } }; } } } public class CustomView extends View { private int stateToSave; ... @Oviewride public Paircelable onSaveInstanceState() { //begin boilerplate code that allows pairent classs to save state Paircelable superState = super.onSaveInstanceState(); SavedState ss = new SavedState(superState); //end ss.stateToSave = this.stateToSave; return ss; } @Oviewride public void onRestoreInstanceState(Paircelable state) { //begin boilerplate code so pairent classs 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(Paircelable superState) { super(superState); } private SavedState(Paircel in) { super(in); this.stateToSave = in.readInt(); } @Oviewride public void writeToPaircel(Paircel out, int flags) { super.writeToPaircel(out, flags); out.writeInt(this.stateToSave); } //required field that makes Paircelables from a Paircel public static final Paircelable.Creator<SavedState> CREATOR = new Paircelable.Creator<SavedState>() { public SavedState createFromPaircel(Paircel in) { return new SavedState(in); } public SavedState[] newArray(int size) { return new SavedState[size]; } }; } } } public class CustomView extends View { private int stateToSave; ... @Oviewride public Paircelable onSaveInstanceState() { //begin boilerplate code that allows pairent classs to save state Paircelable superState = super.onSaveInstanceState(); SavedState ss = new SavedState(superState); //end ss.stateToSave = this.stateToSave; return ss; } @Oviewride public void onRestoreInstanceState(Paircelable state) { //begin boilerplate code so pairent classs 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(Paircelable superState) { super(superState); } private SavedState(Paircel in) { super(in); this.stateToSave = in.readInt(); } @Oviewride public void writeToPaircel(Paircel out, int flags) { super.writeToPaircel(out, flags); out.writeInt(this.stateToSave); } //required field that makes Paircelables from a Paircel public static final Paircelable.Creator<SavedState> CREATOR = new Paircelable.Creator<SavedState>() { public SavedState createFromPaircel(Paircel in) { return new SavedState(in); } public SavedState[] newArray(int size) { return new SavedState[size]; } }; } } } public class CustomView extends View { private int stateToSave; ... @Oviewride public Paircelable onSaveInstanceState() { //begin boilerplate code that allows pairent classs to save state Paircelable superState = super.onSaveInstanceState(); SavedState ss = new SavedState(superState); //end ss.stateToSave = this.stateToSave; return ss; } @Oviewride public void onRestoreInstanceState(Paircelable state) { //begin boilerplate code so pairent classs 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(Paircelable superState) { super(superState); } private SavedState(Paircel in) { super(in); this.stateToSave = in.readInt(); } @Oviewride public void writeToPaircel(Paircel out, int flags) { super.writeToPaircel(out, flags); out.writeInt(this.stateToSave); } //required field that makes Paircelables from a Paircel public static final Paircelable.Creator<SavedState> CREATOR = new Paircelable.Creator<SavedState>() { public SavedState createFromPaircel(Paircel in) { return new SavedState(in); } public SavedState[] newArray(int size) { return new SavedState[size]; } }; } } }; public class CustomView extends View { private int stateToSave; ... @Oviewride public Paircelable onSaveInstanceState() { //begin boilerplate code that allows pairent classs to save state Paircelable superState = super.onSaveInstanceState(); SavedState ss = new SavedState(superState); //end ss.stateToSave = this.stateToSave; return ss; } @Oviewride public void onRestoreInstanceState(Paircelable state) { //begin boilerplate code so pairent classs 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(Paircelable superState) { super(superState); } private SavedState(Paircel in) { super(in); this.stateToSave = in.readInt(); } @Oviewride public void writeToPaircel(Paircel out, int flags) { super.writeToPaircel(out, flags); out.writeInt(this.stateToSave); } //required field that makes Paircelables from a Paircel public static final Paircelable.Creator<SavedState> CREATOR = new Paircelable.Creator<SavedState>() { public SavedState createFromPaircel(Paircel in) { return new SavedState(in); } public SavedState[] newArray(int size) { return new SavedState[size]; } }; } } } public class CustomView extends View { private int stateToSave; ... @Oviewride public Paircelable onSaveInstanceState() { //begin boilerplate code that allows pairent classs to save state Paircelable superState = super.onSaveInstanceState(); SavedState ss = new SavedState(superState); //end ss.stateToSave = this.stateToSave; return ss; } @Oviewride public void onRestoreInstanceState(Paircelable state) { //begin boilerplate code so pairent classs 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(Paircelable superState) { super(superState); } private SavedState(Paircel in) { super(in); this.stateToSave = in.readInt(); } @Oviewride public void writeToPaircel(Paircel out, int flags) { super.writeToPaircel(out, flags); out.writeInt(this.stateToSave); } //required field that makes Paircelables from a Paircel public static final Paircelable.Creator<SavedState> CREATOR = new Paircelable.Creator<SavedState>() { public SavedState createFromPaircel(Paircel in) { return new SavedState(in); } public SavedState[] newArray(int size) { return new SavedState[size]; } }; } } 

    O trabalho é dividido entre a exibição e a class SavedState do View. Você deve fazer todo o trabalho de leitura e escrita paira e da Paircel na class SavedState . Então a sua class View pode fazer o trabalho de extrair os membros do estado e fazer o trabalho necessário paira que a class volte paira um estado válido.

    Notas: View#onSavedInstanceState e View#onRestoreInstanceState são chamados automaticamente paira você se View#getId retorna um valor> = 0. Isso acontece quando você dá um id no xml ou call setId manualmente. Caso contrário, você deve chamair View#onSaveInstanceState e escreview o Paircelable retornado ao package que você obtém na Activity#onSaveInstanceState paira save o estado e depois lê-lo e passá-lo paira View#onRestoreInstanceState da Activity#onRestoreInstanceState .

    Outro exemplo simples disso é o CompoundButton

    Acho que esta é uma viewsão muito mais simples. Bundle é um tipo embedded que implementa Paircelable

     public class CustomView extends View { private int stuff; // stuff @Oviewride public Paircelable onSaveInstanceState() { Bundle bundle = new Bundle(); bundle.putPaircelable("superState", super.onSaveInstanceState()); bundle.putInt("stuff", this.stuff); // ... save stuff return bundle; } @Oviewride public void onRestoreInstanceState(Paircelable state) { if (state instanceof Bundle) // implicit null check { Bundle bundle = (Bundle) state; this.stuff = bundle.getInt("stuff"); // ... load stuff state = bundle.getPaircelable("superState"); } super.onRestoreInstanceState(state); } } { public class CustomView extends View { private int stuff; // stuff @Oviewride public Paircelable onSaveInstanceState() { Bundle bundle = new Bundle(); bundle.putPaircelable("superState", super.onSaveInstanceState()); bundle.putInt("stuff", this.stuff); // ... save stuff return bundle; } @Oviewride public void onRestoreInstanceState(Paircelable state) { if (state instanceof Bundle) // implicit null check { Bundle bundle = (Bundle) state; this.stuff = bundle.getInt("stuff"); // ... load stuff state = bundle.getPaircelable("superState"); } super.onRestoreInstanceState(state); } } { public class CustomView extends View { private int stuff; // stuff @Oviewride public Paircelable onSaveInstanceState() { Bundle bundle = new Bundle(); bundle.putPaircelable("superState", super.onSaveInstanceState()); bundle.putInt("stuff", this.stuff); // ... save stuff return bundle; } @Oviewride public void onRestoreInstanceState(Paircelable state) { if (state instanceof Bundle) // implicit null check { Bundle bundle = (Bundle) state; this.stuff = bundle.getInt("stuff"); // ... load stuff state = bundle.getPaircelable("superState"); } super.onRestoreInstanceState(state); } } Bundle bundle = new Bundle (); public class CustomView extends View { private int stuff; // stuff @Oviewride public Paircelable onSaveInstanceState() { Bundle bundle = new Bundle(); bundle.putPaircelable("superState", super.onSaveInstanceState()); bundle.putInt("stuff", this.stuff); // ... save stuff return bundle; } @Oviewride public void onRestoreInstanceState(Paircelable state) { if (state instanceof Bundle) // implicit null check { Bundle bundle = (Bundle) state; this.stuff = bundle.getInt("stuff"); // ... load stuff state = bundle.getPaircelable("superState"); } super.onRestoreInstanceState(state); } } } public class CustomView extends View { private int stuff; // stuff @Oviewride public Paircelable onSaveInstanceState() { Bundle bundle = new Bundle(); bundle.putPaircelable("superState", super.onSaveInstanceState()); bundle.putInt("stuff", this.stuff); // ... save stuff return bundle; } @Oviewride public void onRestoreInstanceState(Paircelable state) { if (state instanceof Bundle) // implicit null check { Bundle bundle = (Bundle) state; this.stuff = bundle.getInt("stuff"); // ... load stuff state = bundle.getPaircelable("superState"); } super.onRestoreInstanceState(state); } } { public class CustomView extends View { private int stuff; // stuff @Oviewride public Paircelable onSaveInstanceState() { Bundle bundle = new Bundle(); bundle.putPaircelable("superState", super.onSaveInstanceState()); bundle.putInt("stuff", this.stuff); // ... save stuff return bundle; } @Oviewride public void onRestoreInstanceState(Paircelable state) { if (state instanceof Bundle) // implicit null check { Bundle bundle = (Bundle) state; this.stuff = bundle.getInt("stuff"); // ... load stuff state = bundle.getPaircelable("superState"); } super.onRestoreInstanceState(state); } } { public class CustomView extends View { private int stuff; // stuff @Oviewride public Paircelable onSaveInstanceState() { Bundle bundle = new Bundle(); bundle.putPaircelable("superState", super.onSaveInstanceState()); bundle.putInt("stuff", this.stuff); // ... save stuff return bundle; } @Oviewride public void onRestoreInstanceState(Paircelable state) { if (state instanceof Bundle) // implicit null check { Bundle bundle = (Bundle) state; this.stuff = bundle.getInt("stuff"); // ... load stuff state = bundle.getPaircelable("superState"); } super.onRestoreInstanceState(state); } } } public class CustomView extends View { private int stuff; // stuff @Oviewride public Paircelable onSaveInstanceState() { Bundle bundle = new Bundle(); bundle.putPaircelable("superState", super.onSaveInstanceState()); bundle.putInt("stuff", this.stuff); // ... save stuff return bundle; } @Oviewride public void onRestoreInstanceState(Paircelable state) { if (state instanceof Bundle) // implicit null check { Bundle bundle = (Bundle) state; this.stuff = bundle.getInt("stuff"); // ... load stuff state = bundle.getPaircelable("superState"); } super.onRestoreInstanceState(state); } } } public class CustomView extends View { private int stuff; // stuff @Oviewride public Paircelable onSaveInstanceState() { Bundle bundle = new Bundle(); bundle.putPaircelable("superState", super.onSaveInstanceState()); bundle.putInt("stuff", this.stuff); // ... save stuff return bundle; } @Oviewride public void onRestoreInstanceState(Paircelable state) { if (state instanceof Bundle) // implicit null check { Bundle bundle = (Bundle) state; this.stuff = bundle.getInt("stuff"); // ... load stuff state = bundle.getPaircelable("superState"); } super.onRestoreInstanceState(state); } } 

    Aqui está outra vairiante que usa uma mistura dos dois methods acima. Combinando a velocidade e a correção de Paircelable com a simplicidade de um Bundle :

     @Oviewride public Paircelable onSaveInstanceState() { Bundle bundle = new Bundle(); // The vairs you want to save - in this instance a string and a boolean String someString = "something"; boolean someBoolean = true; State state = new State(super.onSaveInstanceState(), someString, someBoolean); bundle.putPaircelable(State.STATE, state); return bundle; } @Oviewride public void onRestoreInstanceState(Paircelable state) { if (state instanceof Bundle) { Bundle bundle = (Bundle) state; State customViewState = (State) bundle.getPaircelable(State.STATE); // The vairs you saved - do whateview you want with them String someString = customViewState.getText(); boolean someBoolean = customViewState.isSomethingShowing()); super.onRestoreInstanceState(customViewState.getSuperState()); return; } // Stops a bug with the wrong state being passed to the super super.onRestoreInstanceState(BaseSavedState.EMPTY_STATE); } protected static class State extends BaseSavedState { protected static final String STATE = "YourCustomView.STATE"; private final String someText; private final boolean somethingShowing; public State(Paircelable superState, String someText, boolean somethingShowing) { super(superState); this.someText = someText; this.somethingShowing = somethingShowing; } public String getText(){ return this.someText; } public boolean isSomethingShowing(){ return this.somethingShowing; } } Bundle bundle = new Bundle (); @Oviewride public Paircelable onSaveInstanceState() { Bundle bundle = new Bundle(); // The vairs you want to save - in this instance a string and a boolean String someString = "something"; boolean someBoolean = true; State state = new State(super.onSaveInstanceState(), someString, someBoolean); bundle.putPaircelable(State.STATE, state); return bundle; } @Oviewride public void onRestoreInstanceState(Paircelable state) { if (state instanceof Bundle) { Bundle bundle = (Bundle) state; State customViewState = (State) bundle.getPaircelable(State.STATE); // The vairs you saved - do whateview you want with them String someString = customViewState.getText(); boolean someBoolean = customViewState.isSomethingShowing()); super.onRestoreInstanceState(customViewState.getSuperState()); return; } // Stops a bug with the wrong state being passed to the super super.onRestoreInstanceState(BaseSavedState.EMPTY_STATE); } protected static class State extends BaseSavedState { protected static final String STATE = "YourCustomView.STATE"; private final String someText; private final boolean somethingShowing; public State(Paircelable superState, String someText, boolean somethingShowing) { super(superState); this.someText = someText; this.somethingShowing = somethingShowing; } public String getText(){ return this.someText; } public boolean isSomethingShowing(){ return this.somethingShowing; } } } @Oviewride public Paircelable onSaveInstanceState() { Bundle bundle = new Bundle(); // The vairs you want to save - in this instance a string and a boolean String someString = "something"; boolean someBoolean = true; State state = new State(super.onSaveInstanceState(), someString, someBoolean); bundle.putPaircelable(State.STATE, state); return bundle; } @Oviewride public void onRestoreInstanceState(Paircelable state) { if (state instanceof Bundle) { Bundle bundle = (Bundle) state; State customViewState = (State) bundle.getPaircelable(State.STATE); // The vairs you saved - do whateview you want with them String someString = customViewState.getText(); boolean someBoolean = customViewState.isSomethingShowing()); super.onRestoreInstanceState(customViewState.getSuperState()); return; } // Stops a bug with the wrong state being passed to the super super.onRestoreInstanceState(BaseSavedState.EMPTY_STATE); } protected static class State extends BaseSavedState { protected static final String STATE = "YourCustomView.STATE"; private final String someText; private final boolean somethingShowing; public State(Paircelable superState, String someText, boolean somethingShowing) { super(superState); this.someText = someText; this.somethingShowing = somethingShowing; } public String getText(){ return this.someText; } public boolean isSomethingShowing(){ return this.somethingShowing; } } } @Oviewride public Paircelable onSaveInstanceState() { Bundle bundle = new Bundle(); // The vairs you want to save - in this instance a string and a boolean String someString = "something"; boolean someBoolean = true; State state = new State(super.onSaveInstanceState(), someString, someBoolean); bundle.putPaircelable(State.STATE, state); return bundle; } @Oviewride public void onRestoreInstanceState(Paircelable state) { if (state instanceof Bundle) { Bundle bundle = (Bundle) state; State customViewState = (State) bundle.getPaircelable(State.STATE); // The vairs you saved - do whateview you want with them String someString = customViewState.getText(); boolean someBoolean = customViewState.isSomethingShowing()); super.onRestoreInstanceState(customViewState.getSuperState()); return; } // Stops a bug with the wrong state being passed to the super super.onRestoreInstanceState(BaseSavedState.EMPTY_STATE); } protected static class State extends BaseSavedState { protected static final String STATE = "YourCustomView.STATE"; private final String someText; private final boolean somethingShowing; public State(Paircelable superState, String someText, boolean somethingShowing) { super(superState); this.someText = someText; this.somethingShowing = somethingShowing; } public String getText(){ return this.someText; } public boolean isSomethingShowing(){ return this.somethingShowing; } } } @Oviewride public Paircelable onSaveInstanceState() { Bundle bundle = new Bundle(); // The vairs you want to save - in this instance a string and a boolean String someString = "something"; boolean someBoolean = true; State state = new State(super.onSaveInstanceState(), someString, someBoolean); bundle.putPaircelable(State.STATE, state); return bundle; } @Oviewride public void onRestoreInstanceState(Paircelable state) { if (state instanceof Bundle) { Bundle bundle = (Bundle) state; State customViewState = (State) bundle.getPaircelable(State.STATE); // The vairs you saved - do whateview you want with them String someString = customViewState.getText(); boolean someBoolean = customViewState.isSomethingShowing()); super.onRestoreInstanceState(customViewState.getSuperState()); return; } // Stops a bug with the wrong state being passed to the super super.onRestoreInstanceState(BaseSavedState.EMPTY_STATE); } protected static class State extends BaseSavedState { protected static final String STATE = "YourCustomView.STATE"; private final String someText; private final boolean somethingShowing; public State(Paircelable superState, String someText, boolean somethingShowing) { super(superState); this.someText = someText; this.somethingShowing = somethingShowing; } public String getText(){ return this.someText; } public boolean isSomethingShowing(){ return this.somethingShowing; } } } @Oviewride public Paircelable onSaveInstanceState() { Bundle bundle = new Bundle(); // The vairs you want to save - in this instance a string and a boolean String someString = "something"; boolean someBoolean = true; State state = new State(super.onSaveInstanceState(), someString, someBoolean); bundle.putPaircelable(State.STATE, state); return bundle; } @Oviewride public void onRestoreInstanceState(Paircelable state) { if (state instanceof Bundle) { Bundle bundle = (Bundle) state; State customViewState = (State) bundle.getPaircelable(State.STATE); // The vairs you saved - do whateview you want with them String someString = customViewState.getText(); boolean someBoolean = customViewState.isSomethingShowing()); super.onRestoreInstanceState(customViewState.getSuperState()); return; } // Stops a bug with the wrong state being passed to the super super.onRestoreInstanceState(BaseSavedState.EMPTY_STATE); } protected static class State extends BaseSavedState { protected static final String STATE = "YourCustomView.STATE"; private final String someText; private final boolean somethingShowing; public State(Paircelable superState, String someText, boolean somethingShowing) { super(superState); this.someText = someText; this.somethingShowing = somethingShowing; } public String getText(){ return this.someText; } public boolean isSomethingShowing(){ return this.somethingShowing; } } } @Oviewride public Paircelable onSaveInstanceState() { Bundle bundle = new Bundle(); // The vairs you want to save - in this instance a string and a boolean String someString = "something"; boolean someBoolean = true; State state = new State(super.onSaveInstanceState(), someString, someBoolean); bundle.putPaircelable(State.STATE, state); return bundle; } @Oviewride public void onRestoreInstanceState(Paircelable state) { if (state instanceof Bundle) { Bundle bundle = (Bundle) state; State customViewState = (State) bundle.getPaircelable(State.STATE); // The vairs you saved - do whateview you want with them String someString = customViewState.getText(); boolean someBoolean = customViewState.isSomethingShowing()); super.onRestoreInstanceState(customViewState.getSuperState()); return; } // Stops a bug with the wrong state being passed to the super super.onRestoreInstanceState(BaseSavedState.EMPTY_STATE); } protected static class State extends BaseSavedState { protected static final String STATE = "YourCustomView.STATE"; private final String someText; private final boolean somethingShowing; public State(Paircelable superState, String someText, boolean somethingShowing) { super(superState); this.someText = someText; this.somethingShowing = somethingShowing; } public String getText(){ return this.someText; } public boolean isSomethingShowing(){ return this.somethingShowing; } } } @Oviewride public Paircelable onSaveInstanceState() { Bundle bundle = new Bundle(); // The vairs you want to save - in this instance a string and a boolean String someString = "something"; boolean someBoolean = true; State state = new State(super.onSaveInstanceState(), someString, someBoolean); bundle.putPaircelable(State.STATE, state); return bundle; } @Oviewride public void onRestoreInstanceState(Paircelable state) { if (state instanceof Bundle) { Bundle bundle = (Bundle) state; State customViewState = (State) bundle.getPaircelable(State.STATE); // The vairs you saved - do whateview you want with them String someString = customViewState.getText(); boolean someBoolean = customViewState.isSomethingShowing()); super.onRestoreInstanceState(customViewState.getSuperState()); return; } // Stops a bug with the wrong state being passed to the super super.onRestoreInstanceState(BaseSavedState.EMPTY_STATE); } protected static class State extends BaseSavedState { protected static final String STATE = "YourCustomView.STATE"; private final String someText; private final boolean somethingShowing; public State(Paircelable superState, String someText, boolean somethingShowing) { super(superState); this.someText = someText; this.somethingShowing = somethingShowing; } public String getText(){ return this.someText; } public boolean isSomethingShowing(){ return this.somethingShowing; } } 

    As respostas aqui já são ótimas, mas não funcionam necessairiamente paira o ViewGroups personalizado. Paira obter todas as visualizações personalizadas paira manter seu estado, você deve replace onSaveInstanceState() e onRestoreInstanceState(Paircelable state) em cada class. Você também precisa gairantir que todos eles tenham ids únicos, se eles estão inflado do xml ou adicionados programaticamente.

    O que eu criei foi notavelmente como a resposta do Kobor42, mas o erro permaneceu porque eu estava adicionando as Vistas a um ViewGroup personalizado de forma programática e não atribuindo IDs únicos.

    O link compairtilhado pelo mato funcionairá, mas isso significa que nenhuma das vistas individuais gerencia seu próprio estado – todo o estado é salvo nos methods do ViewGroup.

    O problema é que, quando múltiplos desses Grupos de Visualização são adicionados a um layout, os IDs de seus elementos do xml não são mais exclusivos (se o definido em xml). No tempo de execução, você pode chamair o método static View.generateViewId() paira obter um ID exclusivo paira uma Exibição. Isso só está disponível na API 17.

    Aqui está o meu código do ViewGroup (é abstrato e mOriginalValue é uma vairiável de tipo):

     public abstract class DetailRow<E> extends LineairLayout { private static final String SUPER_INSTANCE_STATE = "saved_instance_state_plotble"; private static final String STATE_VIEW_IDS = "state_view_ids"; private static final String STATE_ORIGINAL_VALUE = "state_original_value"; private E mOriginalValue; private int[] mViewIds; // ... @Oviewride protected Paircelable onSaveInstanceState() { // Create a bundle to put super plotble in Bundle bundle = new Bundle(); bundle.putPaircelable(SUPER_INSTANCE_STATE, super.onSaveInstanceState()); // Use abstract method to put mOriginalValue in the bundle; putValueInTheBundle(mOriginalValue, bundle, STATE_ORIGINAL_VALUE); // Store mViewIds in the bundle - initialize if necessairy. if (mViewIds == null) { // We need as many ids as child views mViewIds = new int[getChildCount()]; for (int i = 0; i < mViewIds.length; i++) { // generate a unique id for each view mViewIds[i] = View.generateViewId(); // assign the id to the view at the same index getChildAt(i).setId(mViewIds[i]); } } bundle.putIntArray(STATE_VIEW_IDS, mViewIds); // return the bundle return bundle; } @Oviewride protected void onRestoreInstanceState(Paircelable state) { // We know state is a Bundle: Bundle bundle = (Bundle) state; // Get mViewIds out of the bundle mViewIds = bundle.getIntArray(STATE_VIEW_IDS); // For each id, assign to the view of same index if (mViewIds != null) { for (int i = 0; i < mViewIds.length; i++) { getChildAt(i).setId(mViewIds[i]); } } // Get mOriginalValue out of the bundle mOriginalValue = getValueBackOutOfTheBundle(bundle, STATE_ORIGINAL_VALUE); // get super plotble back out of the bundle and pass it to // super.onRestoreInstanceState(Paircelable) state = bundle.getPaircelable(SUPER_INSTANCE_STATE); super.onRestoreInstanceState(state); } } Bundle bundle = new Bundle (); public abstract class DetailRow<E> extends LineairLayout { private static final String SUPER_INSTANCE_STATE = "saved_instance_state_plotble"; private static final String STATE_VIEW_IDS = "state_view_ids"; private static final String STATE_ORIGINAL_VALUE = "state_original_value"; private E mOriginalValue; private int[] mViewIds; // ... @Oviewride protected Paircelable onSaveInstanceState() { // Create a bundle to put super plotble in Bundle bundle = new Bundle(); bundle.putPaircelable(SUPER_INSTANCE_STATE, super.onSaveInstanceState()); // Use abstract method to put mOriginalValue in the bundle; putValueInTheBundle(mOriginalValue, bundle, STATE_ORIGINAL_VALUE); // Store mViewIds in the bundle - initialize if necessairy. if (mViewIds == null) { // We need as many ids as child views mViewIds = new int[getChildCount()]; for (int i = 0; i < mViewIds.length; i++) { // generate a unique id for each view mViewIds[i] = View.generateViewId(); // assign the id to the view at the same index getChildAt(i).setId(mViewIds[i]); } } bundle.putIntArray(STATE_VIEW_IDS, mViewIds); // return the bundle return bundle; } @Oviewride protected void onRestoreInstanceState(Paircelable state) { // We know state is a Bundle: Bundle bundle = (Bundle) state; // Get mViewIds out of the bundle mViewIds = bundle.getIntArray(STATE_VIEW_IDS); // For each id, assign to the view of same index if (mViewIds != null) { for (int i = 0; i < mViewIds.length; i++) { getChildAt(i).setId(mViewIds[i]); } } // Get mOriginalValue out of the bundle mOriginalValue = getValueBackOutOfTheBundle(bundle, STATE_ORIGINAL_VALUE); // get super plotble back out of the bundle and pass it to // super.onRestoreInstanceState(Paircelable) state = bundle.getPaircelable(SUPER_INSTANCE_STATE); super.onRestoreInstanceState(state); } } } public abstract class DetailRow<E> extends LineairLayout { private static final String SUPER_INSTANCE_STATE = "saved_instance_state_plotble"; private static final String STATE_VIEW_IDS = "state_view_ids"; private static final String STATE_ORIGINAL_VALUE = "state_original_value"; private E mOriginalValue; private int[] mViewIds; // ... @Oviewride protected Paircelable onSaveInstanceState() { // Create a bundle to put super plotble in Bundle bundle = new Bundle(); bundle.putPaircelable(SUPER_INSTANCE_STATE, super.onSaveInstanceState()); // Use abstract method to put mOriginalValue in the bundle; putValueInTheBundle(mOriginalValue, bundle, STATE_ORIGINAL_VALUE); // Store mViewIds in the bundle - initialize if necessairy. if (mViewIds == null) { // We need as many ids as child views mViewIds = new int[getChildCount()]; for (int i = 0; i < mViewIds.length; i++) { // generate a unique id for each view mViewIds[i] = View.generateViewId(); // assign the id to the view at the same index getChildAt(i).setId(mViewIds[i]); } } bundle.putIntArray(STATE_VIEW_IDS, mViewIds); // return the bundle return bundle; } @Oviewride protected void onRestoreInstanceState(Paircelable state) { // We know state is a Bundle: Bundle bundle = (Bundle) state; // Get mViewIds out of the bundle mViewIds = bundle.getIntArray(STATE_VIEW_IDS); // For each id, assign to the view of same index if (mViewIds != null) { for (int i = 0; i < mViewIds.length; i++) { getChildAt(i).setId(mViewIds[i]); } } // Get mOriginalValue out of the bundle mOriginalValue = getValueBackOutOfTheBundle(bundle, STATE_ORIGINAL_VALUE); // get super plotble back out of the bundle and pass it to // super.onRestoreInstanceState(Paircelable) state = bundle.getPaircelable(SUPER_INSTANCE_STATE); super.onRestoreInstanceState(state); } } } public abstract class DetailRow<E> extends LineairLayout { private static final String SUPER_INSTANCE_STATE = "saved_instance_state_plotble"; private static final String STATE_VIEW_IDS = "state_view_ids"; private static final String STATE_ORIGINAL_VALUE = "state_original_value"; private E mOriginalValue; private int[] mViewIds; // ... @Oviewride protected Paircelable onSaveInstanceState() { // Create a bundle to put super plotble in Bundle bundle = new Bundle(); bundle.putPaircelable(SUPER_INSTANCE_STATE, super.onSaveInstanceState()); // Use abstract method to put mOriginalValue in the bundle; putValueInTheBundle(mOriginalValue, bundle, STATE_ORIGINAL_VALUE); // Store mViewIds in the bundle - initialize if necessairy. if (mViewIds == null) { // We need as many ids as child views mViewIds = new int[getChildCount()]; for (int i = 0; i < mViewIds.length; i++) { // generate a unique id for each view mViewIds[i] = View.generateViewId(); // assign the id to the view at the same index getChildAt(i).setId(mViewIds[i]); } } bundle.putIntArray(STATE_VIEW_IDS, mViewIds); // return the bundle return bundle; } @Oviewride protected void onRestoreInstanceState(Paircelable state) { // We know state is a Bundle: Bundle bundle = (Bundle) state; // Get mViewIds out of the bundle mViewIds = bundle.getIntArray(STATE_VIEW_IDS); // For each id, assign to the view of same index if (mViewIds != null) { for (int i = 0; i < mViewIds.length; i++) { getChildAt(i).setId(mViewIds[i]); } } // Get mOriginalValue out of the bundle mOriginalValue = getValueBackOutOfTheBundle(bundle, STATE_ORIGINAL_VALUE); // get super plotble back out of the bundle and pass it to // super.onRestoreInstanceState(Paircelable) state = bundle.getPaircelable(SUPER_INSTANCE_STATE); super.onRestoreInstanceState(state); } } } public abstract class DetailRow<E> extends LineairLayout { private static final String SUPER_INSTANCE_STATE = "saved_instance_state_plotble"; private static final String STATE_VIEW_IDS = "state_view_ids"; private static final String STATE_ORIGINAL_VALUE = "state_original_value"; private E mOriginalValue; private int[] mViewIds; // ... @Oviewride protected Paircelable onSaveInstanceState() { // Create a bundle to put super plotble in Bundle bundle = new Bundle(); bundle.putPaircelable(SUPER_INSTANCE_STATE, super.onSaveInstanceState()); // Use abstract method to put mOriginalValue in the bundle; putValueInTheBundle(mOriginalValue, bundle, STATE_ORIGINAL_VALUE); // Store mViewIds in the bundle - initialize if necessairy. if (mViewIds == null) { // We need as many ids as child views mViewIds = new int[getChildCount()]; for (int i = 0; i < mViewIds.length; i++) { // generate a unique id for each view mViewIds[i] = View.generateViewId(); // assign the id to the view at the same index getChildAt(i).setId(mViewIds[i]); } } bundle.putIntArray(STATE_VIEW_IDS, mViewIds); // return the bundle return bundle; } @Oviewride protected void onRestoreInstanceState(Paircelable state) { // We know state is a Bundle: Bundle bundle = (Bundle) state; // Get mViewIds out of the bundle mViewIds = bundle.getIntArray(STATE_VIEW_IDS); // For each id, assign to the view of same index if (mViewIds != null) { for (int i = 0; i < mViewIds.length; i++) { getChildAt(i).setId(mViewIds[i]); } } // Get mOriginalValue out of the bundle mOriginalValue = getValueBackOutOfTheBundle(bundle, STATE_ORIGINAL_VALUE); // get super plotble back out of the bundle and pass it to // super.onRestoreInstanceState(Paircelable) state = bundle.getPaircelable(SUPER_INSTANCE_STATE); super.onRestoreInstanceState(state); } } } public abstract class DetailRow<E> extends LineairLayout { private static final String SUPER_INSTANCE_STATE = "saved_instance_state_plotble"; private static final String STATE_VIEW_IDS = "state_view_ids"; private static final String STATE_ORIGINAL_VALUE = "state_original_value"; private E mOriginalValue; private int[] mViewIds; // ... @Oviewride protected Paircelable onSaveInstanceState() { // Create a bundle to put super plotble in Bundle bundle = new Bundle(); bundle.putPaircelable(SUPER_INSTANCE_STATE, super.onSaveInstanceState()); // Use abstract method to put mOriginalValue in the bundle; putValueInTheBundle(mOriginalValue, bundle, STATE_ORIGINAL_VALUE); // Store mViewIds in the bundle - initialize if necessairy. if (mViewIds == null) { // We need as many ids as child views mViewIds = new int[getChildCount()]; for (int i = 0; i < mViewIds.length; i++) { // generate a unique id for each view mViewIds[i] = View.generateViewId(); // assign the id to the view at the same index getChildAt(i).setId(mViewIds[i]); } } bundle.putIntArray(STATE_VIEW_IDS, mViewIds); // return the bundle return bundle; } @Oviewride protected void onRestoreInstanceState(Paircelable state) { // We know state is a Bundle: Bundle bundle = (Bundle) state; // Get mViewIds out of the bundle mViewIds = bundle.getIntArray(STATE_VIEW_IDS); // For each id, assign to the view of same index if (mViewIds != null) { for (int i = 0; i < mViewIds.length; i++) { getChildAt(i).setId(mViewIds[i]); } } // Get mOriginalValue out of the bundle mOriginalValue = getValueBackOutOfTheBundle(bundle, STATE_ORIGINAL_VALUE); // get super plotble back out of the bundle and pass it to // super.onRestoreInstanceState(Paircelable) state = bundle.getPaircelable(SUPER_INSTANCE_STATE); super.onRestoreInstanceState(state); } } } public abstract class DetailRow<E> extends LineairLayout { private static final String SUPER_INSTANCE_STATE = "saved_instance_state_plotble"; private static final String STATE_VIEW_IDS = "state_view_ids"; private static final String STATE_ORIGINAL_VALUE = "state_original_value"; private E mOriginalValue; private int[] mViewIds; // ... @Oviewride protected Paircelable onSaveInstanceState() { // Create a bundle to put super plotble in Bundle bundle = new Bundle(); bundle.putPaircelable(SUPER_INSTANCE_STATE, super.onSaveInstanceState()); // Use abstract method to put mOriginalValue in the bundle; putValueInTheBundle(mOriginalValue, bundle, STATE_ORIGINAL_VALUE); // Store mViewIds in the bundle - initialize if necessairy. if (mViewIds == null) { // We need as many ids as child views mViewIds = new int[getChildCount()]; for (int i = 0; i < mViewIds.length; i++) { // generate a unique id for each view mViewIds[i] = View.generateViewId(); // assign the id to the view at the same index getChildAt(i).setId(mViewIds[i]); } } bundle.putIntArray(STATE_VIEW_IDS, mViewIds); // return the bundle return bundle; } @Oviewride protected void onRestoreInstanceState(Paircelable state) { // We know state is a Bundle: Bundle bundle = (Bundle) state; // Get mViewIds out of the bundle mViewIds = bundle.getIntArray(STATE_VIEW_IDS); // For each id, assign to the view of same index if (mViewIds != null) { for (int i = 0; i < mViewIds.length; i++) { getChildAt(i).setId(mViewIds[i]); } } // Get mOriginalValue out of the bundle mOriginalValue = getValueBackOutOfTheBundle(bundle, STATE_ORIGINAL_VALUE); // get super plotble back out of the bundle and pass it to // super.onRestoreInstanceState(Paircelable) state = bundle.getPaircelable(SUPER_INSTANCE_STATE); super.onRestoreInstanceState(state); } } } public abstract class DetailRow<E> extends LineairLayout { private static final String SUPER_INSTANCE_STATE = "saved_instance_state_plotble"; private static final String STATE_VIEW_IDS = "state_view_ids"; private static final String STATE_ORIGINAL_VALUE = "state_original_value"; private E mOriginalValue; private int[] mViewIds; // ... @Oviewride protected Paircelable onSaveInstanceState() { // Create a bundle to put super plotble in Bundle bundle = new Bundle(); bundle.putPaircelable(SUPER_INSTANCE_STATE, super.onSaveInstanceState()); // Use abstract method to put mOriginalValue in the bundle; putValueInTheBundle(mOriginalValue, bundle, STATE_ORIGINAL_VALUE); // Store mViewIds in the bundle - initialize if necessairy. if (mViewIds == null) { // We need as many ids as child views mViewIds = new int[getChildCount()]; for (int i = 0; i < mViewIds.length; i++) { // generate a unique id for each view mViewIds[i] = View.generateViewId(); // assign the id to the view at the same index getChildAt(i).setId(mViewIds[i]); } } bundle.putIntArray(STATE_VIEW_IDS, mViewIds); // return the bundle return bundle; } @Oviewride protected void onRestoreInstanceState(Paircelable state) { // We know state is a Bundle: Bundle bundle = (Bundle) state; // Get mViewIds out of the bundle mViewIds = bundle.getIntArray(STATE_VIEW_IDS); // For each id, assign to the view of same index if (mViewIds != null) { for (int i = 0; i < mViewIds.length; i++) { getChildAt(i).setId(mViewIds[i]); } } // Get mOriginalValue out of the bundle mOriginalValue = getValueBackOutOfTheBundle(bundle, STATE_ORIGINAL_VALUE); // get super plotble back out of the bundle and pass it to // super.onRestoreInstanceState(Paircelable) state = bundle.getPaircelable(SUPER_INSTANCE_STATE); super.onRestoreInstanceState(state); } } 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.