1

In my custom view I have:

 @Override
public Parcelable onSaveInstanceState() {
    Parcelable superState = super.onSaveInstanceState();
    return new SavedState(superState,  mSomeBoolean);
}

@Override
public void onRestoreInstanceState(Parcelable state) {
    final SavedState savedState = (SavedState) state;
    super.onRestoreInstanceState(savedState.getSuperState());
    this.mSomeBoolean = savedState.someBoolean;   
}

My SavedState implements BaseSavedState, and the problem is that it appears that writeToParcel(Parcel out, int flags) does not get called, but, strangely enough, the state is correctly saved.

I do not understand why it happens and, more so, how the someBoolean state is saved despite not being written to parcel. Any help or direction would help.


Update:

Here is the SavedState implementation:

  class SavedState extends View.BaseSavedState {  

boolean someBoolean;

SavedState(Parcelable superState, boolean someBoolean){
    super(superState);
    this.someBoolean = someBoolean;   
}

private SavedState(Parcel in) {
    super(in);
    this.someBoolean  = (in.readInt() != 0);      
}

@Override
public void writeToParcel(Parcel out, int flags) {
    super.writeToParcel(out, flags);
    out.writeInt(someBoolean ? 1 : 0);       
}

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

}

Ari
  • 3,086
  • 1
  • 19
  • 27
  • `and the problem is that it appears that writeToParcel(Parcel out, int flags) does not get called` why should it be called? – pskink Nov 29 '15 at 15:10
  • Because it is returned from `onSaveInstanceState()` and, according to the docs, the `View` uses the returned state to save and then restore state. I cannot see the difference here from a regular implementation of `Parcelable` where `writeToParcel` is called. – Ari Nov 29 '15 at 15:44
  • i mean why writeToParcel should be called? – pskink Nov 29 '15 at 15:54
  • In this answer http://stackoverflow.com/a/3542895/4418460, the author implements `writeToParcel` why if it's not going to get called? get me? – Ari Nov 29 '15 at 15:56
  • ok what is your code of `SavedState` ? – pskink Nov 29 '15 at 16:00
  • I updated my question – Ari Nov 29 '15 at 16:06
  • my bad, i mixed up onSaveInstantState with another method of the same name, if you say that your boolean state is preserved then writeToParcel should be of course called as it is the only place where you are writing your boolean to the Parcel, use Log.d to trace it out – pskink Nov 29 '15 at 16:28
  • It is still not called, what a strange thing. `SavedState(Parcel in)` is also not being called. This is very frustrating. – Ari Nov 29 '15 at 16:36
  • i tried your exact code, and yes, `writeToParcel` gets called, i dont know what is your way of debugging, mine is writing `Log.d` calls in the code and watching `adb logcat` output, maybe you are using android studio with some filters on and you dont see it? – pskink Nov 29 '15 at 17:07
  • It is probably something else, because my state is saved even if I comment out the body of `writeToParcel()`, I'll run a thorough investigation later and let u know. Thanks for the help. – Ari Nov 29 '15 at 17:09
  • I'm having the exact same problem – dylan7 Jan 20 '16 at 23:57

0 Answers0