fweigl
fweigl

Reputation: 22038

EditText behaving strange on orientation change

See the following Activity:

public class MainActivity extends ActionBarActivity {

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

        LinearLayout linearLayout = (LinearLayout) findViewById(R.id.root);

        for (int i = 0; i < 8; i++) {

            EditText editText = (EditText) LayoutInflater.from(this).inflate(R.layout.edittextlayout, null);
            editText.setText("#" + i);
            linearLayout.addView(editText);

        }

    }

}

The layout R.layout.activity_main:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/root"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">


    </LinearLayout>

</LinearLayout>

and the layout R.layout.edittext_layout:

<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/editText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

After starting the app it looks like I would expect: every EditText being filled with it's index.

enter image description here

After rotating the device though, the Activity looks like this:

enter image description here

All the EditTexts are there, but they all contain the same text.

What baffles me even more is that this doesn't happen when creating the EditTexts programmatically with

EditText editText = new EditText(this)

instead of inflating it from a layout.

What's happening there?

You can check out my example and try for yourself here.

EDIT: This is not a duplicate of this question as in my case the text in the EditText does not double but get mixed up between different EditTexts.

Upvotes: 2

Views: 625

Answers (2)

Arda Kara
Arda Kara

Reputation: 521

The problem is your activity recreates on every orientation and saved instance data handled wrong by Android. You can avoid it by setting an id to your dynamically created views or you can Override onSaveInstanceState method to save your data like text in your edittexts and recover it on onCreate method like so:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Check if we're recreating
    if (savedInstanceState != null) {
        // Restore value of edittext from saved state
            editText.setText(savedInstanceState.getString("edittext"));
    }
}

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    savedInstanceState.putString("edittext", editText.getText().toString());
    // Always call the superclass so it can save the view hierarchy state
    super.onSaveInstanceState(savedInstanceState);
}

If you encounter any problem similar after doing this try to Override onRestoreInstanceState method to prevent android from doing your work.

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    //do nothing
}

Upvotes: 0

Tronum
Tronum

Reputation: 717

Try to set some ID for each View.

For example:

view.setId(id);  

Or use

onSaveInstanceState() - onRestoreInstanceState()

for saving info.

Upvotes: 4

Related Questions