Search code examples
androidandroid-databindingandroid-architecture-componentsandroidxtwo-way-binding

Two-way DataBinding went wrong after device rotation


I use Android DataBinding Library (Two-way) with LiveData (binding syntax @={})

To reuse UI, I intensively use include layout mechanism when designing layout file. Actually, I include a same layout file multiple times in building a form layout.

Everything gone well until the DEVICE ROTATION. After the device rotates, all the field (editText) get the same value as in the last row (as shown in the picture below).

The problem happens when the activity is re-created after the rotation so I can prevent this by setting for android:configChanges of the activity. But I'm curious about the root of this problem and how to solve its..

You can find the major parts of the source code below or full source code. Thanks in advance.

enter image description here


SOURCE CODE

  • Layout for a row (1 TextView & 1 EditText)

enter image description here

  • Reuse the layout above 2 time in main layout enter image description here

  • ViewModel enter image description here

  • Main activity - Binding in OnCreate enter image description here


Solution

  • To one who may concern about this problem, the reason seems to be related to the ID of editText in form row layout (Layout for a row (1 TextView & 1 EditText)), i.e. android:id="@+id/editTextID" in this case.

    Three rows for first name, last name and password created by using the same row layout so editTexts for these fields haves the same ids.

    After the rotation, the frameworks could notify the changes in edit text of the last row (password) but the two first rows also receive these updates. That probably causes the problem.

    To resolve it, simple remove android:id="@+id/editTextID" in the row layout. There is nothing related to ViewModel or its lifeCycle.