3

解決...

他のコントロールを含む複合ビューがあります。onSaveInstanceStatesaveとをオーバーライドしようとしています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];
        }
    };
}
4

2 に答える 2

6

考え出した...私のカスタムビューには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 の競合を確実に回避するために、カスタム ビューの内部について知る必要があるのはなぜですか?

于 2013-12-06T03:52:04.543 に答える