admin管理员组

文章数量:1122846

I have a layout with some EditText, like name, surname, city... I include this layout in another layout a couple of times, so there is two EditText for the name, surname, etc. The problem I face is if I fill in these fields, for example, in the first EditText for the name I add "Tom", and in the second "John", and then I rotate the device, both EditText have "Tom".

Basically, I am facing the same problem than this user: All EditTexts are populated with data from one EditText after rotation and backpressed

As he said, I could manually add the EditText to the main layour instead of using include but that would make the XML very messy.

I could also try the user solution, but I don't like it much either, and keeping in mind that the question is 10 years old, I am wondering if there is a better solution.

Thank you.

I have a layout with some EditText, like name, surname, city... I include this layout in another layout a couple of times, so there is two EditText for the name, surname, etc. The problem I face is if I fill in these fields, for example, in the first EditText for the name I add "Tom", and in the second "John", and then I rotate the device, both EditText have "Tom".

Basically, I am facing the same problem than this user: All EditTexts are populated with data from one EditText after rotation and backpressed

As he said, I could manually add the EditText to the main layour instead of using include but that would make the XML very messy.

I could also try the user solution, but I don't like it much either, and keeping in mind that the question is 10 years old, I am wondering if there is a better solution.

Thank you.

Share Improve this question asked Nov 21, 2024 at 16:39 xerezxerez 797 bronze badges 1
  • This post answered your question. Please take a look and apply solution to your problem: stackoverflow.com/questions/3542333/… – Son Truong Commented Nov 29, 2024 at 17:21
Add a comment  | 

1 Answer 1

Reset to default 0

The issue may arise in cases where what you are including contains a ViewGroup, in other words, when there is a parent ViewGroup of the EditText where the specific EditText is. If the "EditText" you are including is actually a layout type that is in a ViewGroup, for example, like a TextInputLayout.

Solution 1

If I remember correctly, you can override these methods so they don't try to restore the states.

override fun dispatchSaveInstanceState(container: SparseArray<Parcelable>) {
    // Nothing
}

override fun dispatchRestoreInstanceState(container: SparseArray<Parcelable>) {
    // Nothing
}

Solution 2

Set attribute android:saveEnabled to false and use onSaveInstanceState() or a ViewModel

本文标签: androidEditTexts are populated with same data after rotationStack Overflow