在Android中的方向更改中未调用writeToParcel()

时间:2015-02-25 18:51:09

标签: android parcelable parcel

我希望在方向更改时保存一个Activity和一个Parcelable类。以下是活动中的代码:

Keeper keeper = ...;
@Override
  protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putParcelable("keeper", keeper);
  }

@Override
  protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    if (savedInstanceState != null) {
      keeper = savedInstanceState.getParcelable("keeper");
    }
  }

以下是我的Keeper课程的代码:

public class Keeper implements Parcelable {
  ...

    private int index; 
  @Override
  public int describeContents() {
    return 0;
  }

  @Override
  public void writeToParcel(Parcel dest, int flags) {
    Log.e("QQQQQQQ", "writeToParcel");
    dest.writeInt(index);
  }

  private Keeper(Parcel in) {
    Log.e("QQQQQQQ", "readfromParcel - Keeper");
    index = in.readInt();
  }

  public static final Parcelable.Creator<Keeper> CREATOR
      = new Parcelable.Creator<Keeper>() {
    public Keeper createFromParcel(Parcel in) {
      return new Keeper(in);
    }

    public Keeper[] newArray(int size) {
      return new Keeper[size];
    }
}

我没有看到用writeToParcel和Parcel构造函数编写的日志。我已经尝试关闭应用程序并模拟低内存,以便Android杀死活动,我看到了日志! ! 有谁知道为什么?

0 个答案:

没有答案