解决了…
我有一个包含一些其他控件的复合视图.我试图覆盖保存onSaveInstanceState和onRestoreInstanceState,但我得到一个奇怪的结果.
onRestoreInstanceState的Parcelable状态参数不是我的自定义子类BaseSavedState,SavedState,并且似乎总是BaseSavedState.EMPTY_STATE. (在下面查找“总是失败”的代码注释…
似乎问题可能在于保存部分,因为在onSaveInstanceState进入后没有调用SavedState.writetoParcel.几乎就像调用onSaveInstanceState的人在将结果保持到Parcel之前抛出结果一样.
如果它有所不同,则此视图托管在片段中.
有任何想法吗?
这是我的班级定义:
public class Addressinput extends FrameLayout
这是我的onSaveInstanceState和onRestoreInstanceState对:
@OverrIDeprotected Parcelable onSaveInstanceState(){ // Return saved state Parcelable superState = super.onSaveInstanceState(); return new Addressinput.SavedState( superState, mCurrentLookUp );}@OverrIDeprotected 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自定义复合视图保存和恢复状态不起作用全部内容,希望文章能够帮你解决Android自定义复合视图保存和恢复状态不起作用所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)