且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何防止自定义视图在屏幕方向更改时丢失状态

更新时间:2023-10-24 20:24:40

您可以通过实施 View#onSaveInstanceStateView#onRestoreInstanceState 并扩展 View.BaseSavedState 类.

You do this by implementing View#onSaveInstanceState and View#onRestoreInstanceState and extending the View.BaseSavedState class.

public class CustomView extends View {

  private int stateToSave;

  ...

  @Override
  public Parcelable onSaveInstanceState() {
    //begin boilerplate code that allows parent classes to save state
    Parcelable superState = super.onSaveInstanceState();

    SavedState ss = new SavedState(superState);
    //end

    ss.stateToSave = this.stateToSave;

    return ss;
  }

  @Override
  public void onRestoreInstanceState(Parcelable state) {
    //begin boilerplate code so parent classes can restore state
    if(!(state instanceof SavedState)) {
      super.onRestoreInstanceState(state);
      return;
    }

    SavedState ss = (SavedState)state;
    super.onRestoreInstanceState(ss.getSuperState());
    //end

    this.stateToSave = ss.stateToSave;
  }

  static class SavedState extends BaseSavedState {
    int stateToSave;

    SavedState(Parcelable superState) {
      super(superState);
    }

    private SavedState(Parcel in) {
      super(in);
      this.stateToSave = in.readInt();
    }

    @Override
    public void writeToParcel(Parcel out, int flags) {
      super.writeToParcel(out, flags);
      out.writeInt(this.stateToSave);
    }

    //required field that makes Parcelables from a Parcel
    public static final Parcelable.Creator<SavedState> CREATOR =
        new Parcelable.Creator<SavedState>() {
          public SavedState createFromParcel(Parcel in) {
            return new SavedState(in);
          }
          public SavedState[] newArray(int size) {
            return new SavedState[size];
          }
    };
  }
}

工作分在 View 和 View 的 SavedState 类之间.您应该在 SavedState 类中完成所有读取和写入 Parcel 的工作.然后,您的 View 类可以执行提取状态成员的工作,并执行使类恢复到有效状态所需的工作.

The work is split between the View and the View's SavedState class. You should do all the work of reading and writing to and from the Parcel in the SavedState class. Then your View class can do the work of extracting the state members and doing the work necessary to get the class back to a valid state.

注意:View#onSavedInstanceStateView#onRestoreInstanceState 会自动为您调用,如果 View#getId 返回值 >= 0.这当您在 xml 中给它一个 id 或手动调用 setId 时会发生这种情况.否则,您必须调用 View#onSaveInstanceState 并将 Parcelable 返回到您在 Activity#onSaveInstanceState 中获得的包裹中以保存状态并随后读取它并将其传递给 View#onRestoreInstanceState 来自 Activity#onRestoreInstanceState.

Notes: View#onSavedInstanceState and View#onRestoreInstanceState are called automatically for you if View#getId returns a value >= 0. This happens when you give it an id in xml or call setId manually. Otherwise you have to call View#onSaveInstanceState and write the Parcelable returned to the parcel you get in Activity#onSaveInstanceState to save the state and subsequently read it and pass it to View#onRestoreInstanceState from Activity#onRestoreInstanceState.

另一个简单的例子是 CompoundButton

Another simple example of this is the CompoundButton