Android自定义复合视图保存和恢复状态不起作用
作者:互联网
解决了…
我有一个包含一些其他控件的复合视图.我试图覆盖保存onSaveInstanceState和onRestoreInstanceState,但我得到一个奇怪的结果.
onRestoreInstanceState的Parcelable状态参数不是我的自定义子类BaseSavedState,SavedState,并且似乎总是BaseSavedState.EMPTY_STATE. (在下面查找“总是失败”的代码注释…
似乎问题可能在于保存部分,因为在onSaveInstanceState进入后没有调用SavedState.writeToParcel.几乎就像调用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];
}
};
}
解决方法:
想出来……我的自定义视图与其自定义视图的特定实例使用的FrameLayout具有相同的ID.实例正确保存状态,然后由没有状态持久的FrameLayout覆盖(清除).
我还将其基类更改为更有意义的RelativeView.
在自定义视图的XML中:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:id="@+id/addressInput"
android:background="@drawable/rounded_edit">
和实例用法:
<com.myStuff.AddressInput
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="@+id/addressInput"
android:layout_marginTop="10dp"
app:addressMode="Domestic"
app:showSelect="true"
app:showClear="true" />
更改为:(@ id / addressInput – > @ id / shippingAddress)
<com.myStuff.AddressInput
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="@+id/shippingAddress"
android:layout_marginTop="10dp"
app:addressMode="Domestic"
app:showSelect="true"
app:showClear="true" />
让你希望有一些范围确定ID来防止这种事情.为什么您必须了解自定义视图的内部结构以确保避免ID冲突?
标签:android,custom-view,state,android-custom-view,onrestoreinstancestate 来源: https://codeday.me/bug/20190725/1532231.html