これは正常な動作ではありません。
何よりもまずEditText
、レイアウト XML でコントロールに ID が割り当てられていることを確認します。
編集 1: ID、ピリオドが必要です。これをプログラムで行っている場合は、ID がない限り状態が失われます。
したがって、これを簡単で汚い例として使用します。
// Find my layout
LinearLayout mLinearLayout = (LinearLayout) findViewById(R.id.ll1);
// Add a new EditText with default text of "test"
EditText testText = new EditText(this.getApplicationContext());
testText.setText("test");
// This line is the key; without it, any additional text changes will
// be lost on rotation. Try it with and without the setId, text will revert
// to just "test" when you rotate.
testText.setId(100);
// Add your new EditText to the view.
mLinearLayout.addView(testText);
それはあなたの問題を解決します。
それが失敗した場合は、自分で状態を保存して復元する必要があります。
onSaveInstanceState
次のようにオーバーライドします。
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("textKey", mEditText.getText().toString());
}
そして、復元しOnCreate
ます:
public void onCreate(Bundle savedInstanceState) {
if(savedInstanceState != null)
{
mEditText.setText(savedInstanceState.getString("textKey"));
}
}
android:configChanges="orientation"
また、これを達成しようとして使用しないでください。それは間違った方法です。