解決...
他のコントロールを含む複合ビューがあります。onSaveInstanceState
saveとをオーバーライドしようとしていますonRestoreInstanceState
が、奇妙な結果が得られます。
へのParcelable state
引数onRestoreInstanceState
は、 のカスタム サブクラスではなく、BaseSavedState
常にのSavedState
ようですBaseSavedState.EMPTY_STATE
。(以下の「常に失敗する」コードのコメントを探してください...
SavedState.writeToParcel
の後に呼び出されていないため、問題は保存部分にある可能性が高いonSaveInstanceState enters.
ようonSaveInstanceState
ですParcel
.
違いがある場合、このビューはフラグメント内でホストされます。
何か案は?
これが私のクラス定義です:
public class AddressInput extends FrameLayout
これが私のonSaveInstanceState
とのonRestoreInstanceState
ペアです:
@Override
protected Parcelable onSaveInstanceState()
{
// Return saved state
Parcelable superState = super.onSaveInstanceState();
return new AddressInput.SavedState( superState, mCurrentLookUp );
}
@Override
protected void onRestoreInstanceState( Parcelable state )
{
// **** (state == BaseSavedState.EMPTY_STATE) is also always true
// Cast state to saved state
if ( state instance of AddressInput.SavedState ) // **** <--- always fails
{
AddressInput.SavedState restoreState = (AddressInput.SavedState)state;
// Call super with its portion
super.onRestoreInstanceState( restoreState.getSuperState() );
// Get current lookup
mCurrentLookUp = restoreState.getCurrentLookup();
}
else
// Just send to super
super.onRestoreInstanceState( state );
}
これが私のカスタムBaseSavedState
サブクラス(の内部クラスAddressInput
)です:
public static class SavedState extends BaseSavedState
{
private String mCurrentLookup;
public SavedState(Parcelable superState, String currentLookup)
{
super(superState);
mCurrentLookup = currentLookup;
}
private SavedState(Parcel in)
{
super(in);
this.mCurrentLookup = in.readString();
}
public String getCurrentLookup()
{
return mCurrentLookup;
}
@Override
public void writeToParcel(Parcel out, int flags)
{
super.writeToParcel(out, flags);
out.writeString( this.mCurrentLookup );
}
public static final Parcelable.Creator<SavedState> CREATOR = new Parcelable.Creator<SavedState>()
{
public AddressInput.SavedState createFromParcel(Parcel in)
{
return new AddressInput.SavedState(in);
}
public AddressInput.SavedState[] newArray(int size) {
return new AddressInput.SavedState[size];
}
};
}