简体   繁体   English

覆盖onSaveInstanceState

[英]Overriding onSaveInstanceState

I'm trying to come to grips with the onSaveInstanceState method in class View (not the one in class Activity ). 我正在尝试处理View类中onSaveInstanceState方法 (而不是Activity类中的那个 )。 That method does return a Parcelable . 该方法确实返回一个Parcelable I derived my own View from ViewGroup and overrode that method to save my own state. 我从ViewGroup派生了自己的View ,并覆盖了该方法以保存自己的状态。 But when the state was to be saved I got an exception: 但是当要保存状态时,我遇到了一个例外:

java.lang.IllegalStateException:
Derived class did not call super.onSaveInstanceState()

That is true enough, but simply calling that method doesn't seem enough to me. 的确足够,但是仅调用该方法对我来说似乎还不够。 So how should I do this? 那我该怎么做呢? If the method would get passed a Parcel to write to, I could simply pass that same parcel to the super class, so things would get written sequentially. 如果该方法将传递一个要写入的Parcel ,我可以简单地将该同一个包裹传递给超类,因此事情将按顺序写入。 But this is a return value. 但这是一个返回值。

Should I include this returned object as a member of my own Parcelable representation, and use Parcel.writeParcelable to marshal it along with my own data if needed? 我应该包括这个返回的对象我自己的成员Parcelable表示,并使用Parcel.writeParcelable如果需要用自己的数据编组它一起? Or is there some better way to handle parent invocation and chaining of parcelable objects? 还是有一些更好的方法来处理父级调用和可包裹对象的链接? If so, what class loader should I use when loading the instance state of the super class? 如果是这样,那么在加载超类的实例状态时应该使用什么类加载器?

Since zapl didn't turn his comment into an answer, I'm doing so. 由于zapl并未将他的评论转化为答案,因此我正在这样做。

is there some better way to handle parent invocation and chaining of parcelable objects? 有什么更好的方法来处理父级调用和可包裹对象的链接?

The canonical way to accomplish this is by having your own class for saved data derived from View.BaseSavedState , which in turn is derived from AbsSavedState . 达到此目的的典型方法是拥有一个自己的类,用于保存从View.BaseSavedState派生的已保存数据, View.BaseSavedState又从AbsSavedState派生。 You can call the onSaveInstance handler of the parent class and pass the resulting object to the constructor of your own class. 您可以调用父类的onSaveInstance处理函数,并将结果对象传递给您自己的类的构造函数。 When restoring the data, getSuperState gives the instance aimed at the parent class. 还原数据时, getSuperState给出针对父类的实例。

A typical code example could look like this: 一个典型的代码示例如下所示:

static class SavedState extends View.BaseSavedState {
  // include own data members here
  public SavedState(Parcelable superState) {
    super(superState);
  }
  private SavedState(Parcel in) {
    super(in);
    // read own data here
  }
  @Override public void writeToParcel(Parcel out, int flags) {
    super.writeToParcel(out, flags);
    // write own data here
  }
  public static final Parcelable.Creator<SavedState> CREATOR =
      new Parcelable.Creator<SavedState>() {
    public SavedState createFromParcel(Parcel in) { return SavedState(in); }
    public SavedState[] newArray(int size) { return new SavedState[size]; }
  };
}

@Override public Parcelable onSaveInstanceState() {
  SavedState state = new SavedState(super.onSaveInstanceState());
  // set data members here
  return state;
}

@Override public void onRestoreInstanceState(Parcelable parcelable) {
  SavedState state = (SavedState)parcelable;
  super.onRestoreInstanceState(state.getSuperState());
  // restore from data members here
}

The above was adapted from this presentation by Cyril Mottier, but should also be a close match to how designers intended the use of this class in general. 上面的内容是根据Cyril Mottier的演讲改编而成的,但也应该与设计人员通常打算使用此类的方式非常匹配。

Should I include this returned object as a member of my own Parcelable representation, and use Parcel.writeParcelable to marshal it along with my own data if needed? 我应该包括这个返回的对象我自己的成员Parcelable表示,并使用Parcel.writeParcelable如果需要用自己的数据编组它一起?

Although the mentioned described above seems to be preferred, behind the scenes it does rely on writeParcelable as well. 尽管上面提到的方法似乎是首选,但在后台它也确实依赖于writeParcelable So if there are reasons to not use that base class, simply calling writeParcelable to store the state of the super class should be fine. 因此,如果有理由不使用该基类,只需调用writeParcelable来存储超类的状态就可以了。

what class loader should I use when loading the instance state of the super class? 加载超类的实例状态时应该使用什么类加载器?

The current implementation of AbsSavedState does use null as the class loader argument, causing the use of the default class loader. AbsSavedState当前实现确实使用null作为类加载器参数,从而导致使用默认的类加载器。 However, that line of code is marked with a FIXME comment, so it might change one day. 但是,该行代码标记有FIXME注释,因此可能有一天会更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM