How to retain EditText data on orientation change?

38,099

Solution 1

in onConfigurationChanged method, first get the data of both the edit texts in global variables and then call setContentView method. Now set the saved data again into the edit texts.

Solution 2

By default, Edittext save their own instance when changing orientation.

Be sure that the 2 Edittexts have unique IDs and have the same IDs in both Layouts.

That way, their state should be saved and you can let Android handle the orientation change.

If you are using a fragment, be sure it has a unique ID also and you dont recreate it when recreating the Activity.

Solution 3

A better approach is to let android handle the orientation change. Android will automatically fetch the layout from the correct folder and display it on the screen. All you need to do is to save the input values of the edit texts in the onSaveInsanceState() method and use these saved values to initialize the edit texts in the onCreate() method.
Here is how you can achieve this:

@Override
protected void onCreate (Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.login_screen);
    ...
    ...
    String userName, password;
    if(savedInstanceState!=null)
    {
        userName = savedInstanceState.getString("user_name");
        password= savedInstanceState.getString("password");
    }

    if(userName != null)
        userNameEdtTxt.setText(userName);
    if(password != null)
        passEdtTxt.setText(password);
}

>

@Override
    protected void onSaveInstanceState (Bundle outState)
    {
        outState.putString("user_name", userNameEdtTxt.getText().toString());
        outState.putString("password",  passEdtTxt.getText().toString());
    }

Solution 4

Give the element an id and Android will manage it for you.

android:id="@id/anything"

Solution 5

Im restoring instance to restore values and it works fine for me :)

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.addtask2);
    if(savedInstanceState!=null)
     onRestoreInstanceState(savedInstanceState);

}
Share:
38,099
Yogesh Somani
Author by

Yogesh Somani

A dreamer, a foodie and sometimes... an Android Developer.

Updated on March 17, 2020

Comments

  • Yogesh Somani
    Yogesh Somani about 4 years

    I have a Login screen which consists of 2 EditTexts for Username and Password. My requirement is that on orientation change , input data(if any) in EditText should remain as it is and a new layout should also be drawn. I have 2 layout xml files- one in layout folder and other in layout-land folder. I am trying to implement following 2 approaches but none of them is perfect:

    (1) configChanges:keyboardHidden - In this approach, I don't provide "orientation" in configChanges in manifest file. So I call setContentView() method in both onCreate() and onConfigurationChanged() methods. It fulfills both my requirements. Layout is changed and input data in EditTexts also remains as it is. But it has a big problem :

    When user clicks on Login button, a ProgressDialog shows until server-response is received. Now if user rotates the device while ProgressDialog is running, app crashes. It shows an Exception saying "View cannot be attached to Window." I have tried to handle it using onSaveInstanceState (which DOES get called on orientation change) but app still crashes.

    (2) configChanges:orientation|keyboardHidden - In this approach, I provide "orientation" in manifest. So now I have 2 scenarios:

    (a) If I call setContentView() method in both onCreate() and onConfigurationChanged(), Layout is changed accordingly but EditText data is lost.

    (b) If I call setContentView() method in onCreate() , but not in onConfigurationChanged(), then EditText data is not lost but layout also not changes accordingly.

    And in this approach, onSaveInstanceState() is not even called.

    So I am in a really intimidating situation. Is there any solution to this problem? Please help. Thanx in advance.

  • Yogesh Somani
    Yogesh Somani over 11 years
    If this was enough I woudn't even ask this question. Please read the question more carefully.
  • zapl
    zapl over 11 years
    don't use static to retain references, Activity#onRetainNonConfigurationInstance() or the retain fragment approach described here exist for that purpose
  • Graeme
    Graeme over 10 years
    What do you mean when you say make sure a "fragment has a unique ID"?
  • Yalla T.
    Yalla T. over 10 years
    Every View has an ID ( EditText and Fragments are both Views). You want to make sure that this ID is not used by any other View, because this can lead to problems when trying to save&restore the state automatically.
  • Sharp Steel Software
    Sharp Steel Software almost 10 years
    What if you use a ListView? Are you out of luck because each item does not have a unique ID?
  • Saqib
    Saqib about 9 years
    In case EditText is constructed in java code then id can be set by predefined resource xml id <item type= "id" name="some_id"> an in java editText.setId(R.id.some_id)
  • Shivang
    Shivang over 8 years
    I create two xml files, one in activity_main other is activity_main_land. Both have 2 Edit text fields. Both have ids and in both xml ids are same. When onConfigchange() method is called I change xml file. Now when I rotate device Edit text lost the data. Can you Please help me to get rid this problem.
  • Anonsage
    Anonsage about 8 years
    @Shivang, for that specific case, you should have your activity_main layout in the res/layout folder and another activity_main layout (same name) in the res/layout-land. The system will then take care of switching between the two for you. And, you wouldn't need to add anything to the AndroidManifest.
  • Tamim Attafi
    Tamim Attafi about 6 years
    what if i'm handling oriantation from android manifest, with onConfigurationChanged() method, will onCreate be called again?
  • Ted Henry
    Ted Henry over 5 years
    Does this preserve cursor location and text selection in each EditText? The unique ID approach described by Yalla T. does preserve cursor location and text selection.
  • Ted Henry
    Ted Henry over 5 years
    Does this preserve cursor location and text selection in each EditText? The unique ID approach described by Yalla T. does preserve cursor location and text selection.
  • Rohit Singh
    Rohit Singh about 5 years
    It's not always true. If you override onSaveInstanceState() method and forgot to call super.onSaveInstanceState() then the default behaviour will not work. Read here in more detail stackoverflow.com/questions/151777/…