在 Bundle 测试中设置 EditText 不起作用

Setting EditText in Bundle test not working

嗨,我正在练习 Bundle savedInstanceState 如何在 Activity 创作和修复中发挥作用。我试过这个:

private EditText mTextBox;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTextBox = (EditText) findViewById(R.id.etName);
        mTextBox.setText("hello");
        if(savedInstanceState != null){
            Toast.makeText(this, savedInstanceState.getString("name"), 
                       Toast.LENGTH_SHORT).show();
        mTextBox.setText(savedInstanceState.geteString("name"));
    }
}

  @Override
  protected void onSaveInstanceState(Bundle outState) {
      outState.putString("name", "Joe");
      super.onSaveInstanceState(outState);
}

首先 onCreate() 显然这会将 EditText 字段设置为 "hello" 因为 savedInstanceState 为空 if 块将不会被执行。当我改变方向时,Activity 会经历所有回调并在 if 块中烘烤字符串,但是,它不会将 mTextBox 设置为传入的 Bundle 中的值. EditText 仍然设置为 hello 而不是 Joe,但是,if 块中的 Toast 显示 Joe.

谁能指出为什么这不符合我的预期?

谢谢

您需要设置不同的文本 "hello"。看例子

private EditText mTextBox;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mTextBox = (EditText) findViewById(R.id.etName);
    mTextBox.setText("hello");
    if(savedInstanceState != null){
        Toast.makeText(this, savedInstanceState.getString("name"), 
                   Toast.LENGTH_SHORT).show();
    mTextBox.setText(savedInstanceState.getString("name"));
}
}

@Override
protected void onSaveInstanceState(Bundle outState) {
  mTextBox.setText("Joe");
  outState.putString("name", mTextBox.getText().toString());
  super.onSaveInstanceState(outState);
}

或者您必须覆盖 onRestoreInstanceState。文本未更改时不调用 onCreate。

  @Override
  protected void onRestoreInstanceState(Bundle savedInstanceState) {
    if(savedInstanceState != null){
        mTextBox.setText(savedInstanceState.getString("name"));
    }
  }

这是 TextView.getFreezesText 的结果,它将:

Return whether this text view is including its entire text contents in frozen icicles. For EditText it always returns true.

还有一些来自 TextView.setFreezesText 的更多信息:

Control whether this text view saves its entire text contents when freezing to an icicle, in addition to dynamic state such as cursor position. By default this is false, not saving the text. Set to true if the text in the text view is not being saved somewhere else in persistent storage (such as in a content provider) so that if the view is later thawed the user will not lose their data. For EditText it is always enabled, regardless of the value of the attribute.

icicles指的是savedInstanceState,以前就是这么叫的

如果您想自己保存和恢复文本,您可以创建自定义 EditText 并覆盖 getFreezesText,例如:

public class NonFreezingEditText extends AppCompatEditText {

    public NonFreezingEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean getFreezesText() {
        return false;
    }

}

您也可以使用 View.post:

mTextBox.post(() -> mTextBox.setText(savedInstanceState.getString("name")));

Activity.onRestoreInstanceState

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    mTextBox.setText(savedInstanceState.getString("name"));
}