androidandroid-edittextorientationonconfigurationchanged

EditText data is lost on rotating the device


OK guys, I am done with it. I have been googling for last 2 days but I can't find the exact solution. Everyone is talking about configChanges and all those cliche things which do not seem to work in my case .

I have a login screen which consists of 2 EditTexts. Now this login screen has different layout for both Portrait and Landscape orientation . So I had to create one login.xml in layout folder and another login.xml in layout-land folder. And to support orientation changes I overrid onConfigurationChanged() method in LoginActivity class. In this method I call setContentView(R.layout.login) method so that appropriate login.xml is set as layout fro every orientation.

After all this I defined following in my manifest file as well:

android:configChanges="orientation|keyboardHidden"

But still I am facing the famous old problem. If there is any text in the Edittext and the device is rotated, that text is lost. I don't want to lose that text. Is it possible or not? I have read that we can do it using onSaveInstanceState(Bundle savedInstanceState) method and I have even tried that too but it din't work for me. Please help. Thanks in advance.


Solution

  • Use the put methods to store values in onSaveInstanceState:

    protected void onSaveInstanceState(Bundle extra) {
      super.onSaveInstanceState(extra);
      extra.putString("text", "your text here");
    }
    

    And restore the values in onCreate:

    public void onCreate(Bundle extra) {
      if (extra != null) {
        String value = extra.getString("text");
      }
    }
    

    EDIT(What actually worked):

    try to delete android:configChanges="orientation|keyboardHidden" from manifest.

    Good luck!