解决了...
我有一个包含其他一些控件的复合视图。我试图覆盖 saveonSaveInstanceState
和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];
}
};
}