3

解决了...

我有一个包含其他一些控件的复合视图。我试图覆盖 saveonSaveInstanceStateonRestoreInstanceState,但得到一个奇怪的结果。

Parcelable state论点onRestoreInstanceState不是我的自定义子类BaseSavedStateSavedState而且似乎总是BaseSavedState.EMPTY_STATE。(在下面寻找“总是失败”的代码注释......

似乎问题可能出在保存部分,因为在SavedState.writeToParcel之后没有调用onSaveInstanceState enters. 几乎就像调用onSaveInstanceState的人在将结果持久保存到Parcel.

如果有所不同,则此视图托管在片段中。

有任何想法吗?

这是我的类定义:

public class AddressInput extends FrameLayout

这是我的onSaveInstanceStateonRestoreInstanceState对:

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

2 回答 2

6

想通了...我的自定义视图的 ID 与FrameLayout用于自定义视图的特定实例的 ID 相同。状态被实例正确保存,然后被FrameLayout没有状态持续存在的实例覆盖(清除)。

我还将它的基类更改为RelativeView更有意义的 a。

在自定义视图的 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 冲突?

于 2013-12-06T03:52:04.543 回答
2

很高兴看到您解决了问题。

对于您关于自定义合成视图的问题,我认为可以对自定义视图的 XML 文件进行改进:

使用“合并”标签,而不是任何实际的布局标签(例如:RelativeLayout)作为根元素。

本博客所述,这将防止视图冗余。此外,您不需要为自定义视图分配任何 id,因此可以避免 id 冲突。

对不起,我没有足够的声誉来添加评论,所以我再写一篇文章。

于 2014-02-17T17:00:32.000 回答