1

Steps are as follows: 1. Run app -> OK, EditText set to "hello" 2. Clear text manually 3. Rotate the device

What I expect is that EditText is set again to "hello" because activity is recreated after rotation. However, EditText is empty. Code:

public class MainActivity extends AppCompatActivity {

EditText editText;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    editText = (EditText) findViewById(R.id.editText1);
    editText.setText("hello");
}
}
jreft56
  • 199
  • 1
  • 12

5 Answers5

4

Try to add the following to your EditText specification in xml layout:

android:saveEnabled="false"
algrid
  • 5,600
  • 3
  • 34
  • 37
2

You have to manually do so using saved instance state. It is not about just saving Hello. This is a very common use case. Suppose the user types in something, and then rotates the phone. If due to any reason, the text goes away, the user has to type everything. So you have to save the text somewhere.

https://developer.android.com/guide/topics/resources/runtime-changes.html

So what you have to do is:

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    //Here savedStateValue is a global string
    savedStateValue = //Get the Text From Edit Text here;
    outState.putString("savedStateKEY", savedStateValue);
}

and in onCreate, do something like this:

    if (savedInstanceState != null) {
        //Restore the Text to EditText
        editText.setText(savedInstanceState.getString("savedStateKEY"));
    }

EDIT The second part is why is it happening. It because, you are removing the "Hello" text and on screen orientation change, it is restoring the data from the Bundle that was passed from initial state. In that bundle, there is no "Hello" because you have removed it manually. So if you are using

android:saveEnabled="false"

Then you are making android not to save any state in the EditText.

Aman
  • 149
  • 13
  • This is the only answer here the addresses the real issue. All the other answers involving configChanges are just telling you to avoid the issue by not allowing the view to rotate. You'd be much better off just learning how to restore state as mentioned in this answer. – Michael Peterson Dec 20 '17 at 17:28
0

Add configChanges constraint in your manifest file for that activity.

Mukesh Kumar
  • 221
  • 2
  • 8
0

add in your manifest.xml file for that Activity

      <activity
        android:name=".SignUpActivity"
        android:configChanges="orientation|screenSize|keyboardHidden"      
        android:label="@string/app_name"
       />
J.D.
  • 109
  • 1
  • 11
-1

EditText is persistent on device orientation change, you need to use TextView instead

VK.N
  • 193
  • 1
  • 2
  • 12
  • Not helpful. TextView is not a replacement for EditText. If you are using EditText you probably want to be able to, you know, edit the text. The answer here is to restore the state of the EditText. See Aman's answer for a start. – Michael Peterson Dec 20 '17 at 17:30