Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EditText not automatically saved on screen orientation change

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).

like image 929
Luis Mendo Avatar asked Oct 07 '13 20:10

Luis Mendo


People also ask

How do you change orientation when retaining data?

Another most common solution to dealing with orientation changes by setting the android:configChanges flag on your Activity in AndroidManifest. xml. Using this attribute your Activities won't be recreated and all your views and data will still be there after orientation change.

How do I change the cursor position in EditText?

Say in your xml's edittext section, add android:paddingLeft="100dp" This will move your start position of cursor 100dp right from left end. Same way, you can use android:paddingRight="100dp" This will move your end position of cursor 100dp left from right end.

How can I make my spinner look like EditText?

simple_spinner_dropdown_item is used for dropdown item and custom layout custom_spinner_item is used for spinner view to show only TextView . FYI, You can customize this TextView as per your needs.


1 Answers

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.

like image 94
Mike P. Avatar answered Sep 21 '22 16:09

Mike P.