3

我正在尝试创建一个自定义视图来保存它的状态。当我尝试旋转它时,它调用了 onSaveInstanceState 但从不调用 writeToParcel。

这是 onSaveInstanceState :

@Override
public Parcelable onSaveInstanceState()
{
    Logger.d("On Save Instance State");

    Parcelable superState = super.onSaveInstanceState();

    PictureItemState pis = new PictureItemState(superState);

    for (PictureItem item : mImageList)
    {
        if (!item.isButton)
        {
            pis.mUriList.add(item.uri.toString());
            pis.mIndexList.add(item.index);
        }
    }

    return pis;
}

这是实现 writeToParcel 的状态类:

static class PictureItemState extends BaseSavedState
{
...    
@Override
    public void writeToParcel(Parcel dest, int flags)
    {
        super.writeToParcel(dest, flags);

        Logger.d("Write to parcel");

        int size = mUriList.size();

        dest.writeInt(size);

        for (int i = 0; i < size; i++)
        {
            dest.writeString(mUriList.get(i));
            dest.writeInt(mIndexList.get(i));
        }
    }
...
}
4

0 回答 0