EditText not automatically saved on screen orienta

2019-01-11 19:54发布

I read that Android automatically saves the content of EditText objects when an application is about to be stopped or killed. However, in my app the content of an EditText is lost when screen orientation changes.

Is it normal behaviour? Do I then have to manually save/restore its content with onSaveInstanceState/onRestoreInstanceState? Or is there an easier method to tell Android to save it end restore it?

Edit:

I create the EditText object programmatically, not in XML. This turns out to be related to the problem (see accepted answer below).

4条回答
Emotional °昔
2楼-- · 2019-01-11 20:15

The easiest way I found to save an object on onSaveInstanceState is to implement serializable and put in bundle

@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putSerializable("myObj", myObj);
}

where myObj class implements serializable and in onCreate() method

if (savedInstanceState != null && savedInstanceState.getSerializable("myObj") != null) {
myObj = ((MyObj) savedInstanceState.getSerializable("myObj"));
}
查看更多
虎瘦雄心在
3楼-- · 2019-01-11 20:21

One possible cause is that you override onSaveInstanceState but you forget to call the same for super class

super.onSaveInstanceState(outState);

State of all views in activity is auto saved UNLESS you override this functionality. Even if this is obvious, mistakes are possible.

查看更多
Juvenile、少年°
4楼-- · 2019-01-11 20:26

could you use android:freezesText="true" in the xml layout?

查看更多
乱世女痞
5楼-- · 2019-01-11 20:41

This is not normal behavior.

First and foremost, ensure that you have IDs assigned to your EditText controls in the layout XML.

Edit 1: It just needs an ID, period. If you're doing this programmatically, it will lose state unless it has an ID.

So using this as a quick & dirty example:

    // 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);

That will solve your problem.

Should that fail, you'll need to save and restore state yourself.

Override onSaveInstanceState like so:

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putString("textKey", mEditText.getText().toString());
}

And then restore in OnCreate:

public void onCreate(Bundle savedInstanceState) {
    if(savedInstanceState != null)
    {
        mEditText.setText(savedInstanceState.getString("textKey"));
    }
}

Also, please don't use android:configChanges="orientation" to try to accomplish this, it's the wrong way to go.

查看更多
登录 后发表回答