如何$来自全国各地的屏幕方向的变化失去状态P $ pvent自定义视图 [英] How to prevent custom views from losing state across screen orientation changes

查看:132
本文介绍了如何$来自全国各地的屏幕方向的变化失去状态P $ pvent自定义视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经成功地实施<一个href="http://developer.android.com/reference/android/app/Activity.html#onRetainNonConfigurationInstance%28%29"><$c$c>onRetainNonConfigurationInstance()我的主活动来保存和恢复整个屏幕方向改变某些关键组件。

I've successfully implemented onRetainNonConfigurationInstance() for my main Activity to save and restore certain critical components across screen orientation changes.

但现在看来,我的自定义视图正在从头开始时的方向变化重新创建。这是有道理的,但在我的情况下,它是不方便的,因为有问题的自定义视图是一个X / Y情节和绘制点都存储在自定义视图。

But it seems, my custom views are being re-created from scratch when the orientation changes. This makes sense, although in my case it's inconvenient because the custom view in question is an X/Y plot and the plotted points are stored in the custom view.

有没有实施类似的东西 onRetainNonConfigurationInstance()为自定义视图狡猾的方式,还是我只是需要实现自定义视图方法,它们让我获取和设置其状态?

Is there a crafty way to implement something similar to onRetainNonConfigurationInstance() for a custom view, or do I need to just implement methods in the custom view which allow me to get and set its "state"?

推荐答案

您做到这一点通过实施<一个href="http://developer.android.com/reference/android/view/View.html#onSaveInstanceState%28%29"><$c$c>View#onSaveInstanceState和<一href="http://developer.android.com/reference/android/view/View.html#onRestoreInstanceState%28android.os.Parcelable%29"><$c$c>View#onRestoreInstanceState和延伸<一href="http://developer.android.com/reference/android/view/View.BaseSavedState.html"><$c$c>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];
          }
    };
  }
}

这项工作是分割的视图和视图的SavedState类之间。你应该做的阅读和写作的全部工作,并从包裹 SavedState 类。然后您的视图类可以做提取状态成员和做必要的工作来获取类返回一个有效的工作状态。

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.

注:查看#onSavedInstanceState 查看#onRestoreInstanceState 自动如果要求你查看#的getId 返回一个值> = 0。这发生在你给它一个id XML或致电 SETID 手动。否则,你要叫查看#的onSaveInstanceState 并写入Parcelable返回时活动#的onSaveInstanceState 拿到包裹保存状态,随后读它,它从活动#onRestoreInstanceState 传递给查看#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.

这样的另一个简单的例子是<一个href="http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/4.0.3_r1/android/widget/CompoundButton.java#CompoundButton.onSaveInstanceState%28%29"><$c$c>CompoundButton

Another simple example of this is the CompoundButton

这篇关于如何$来自全国各地的屏幕方向的变化失去状态P $ pvent自定义视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆