I used multiple include tags in a xml layout like this:
<include
android:id="@+id/firstView"
layout="@layout/layout_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="@dimen/normal_margin"
android:layout_marginTop="@dimen/normal_margin" />
<include
android:id="@+id/secondView"
layout="@layout/layout_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="@dimen/normal_margin"
android:layout_marginTop="@dimen/normal_margin" />
both include tags are the same and they are just an EditText and a Label for it.
their input data updates programmatically in a kotlin activity, everything works fine and views update correctly but when I changed device theme (configuration change) after calling onCreate , suddenly both include views (firstView , secondView) have same values (value of secondView)
UPDATE:
I'm using viewBinding to update views:
firstView.editText.setText(firstItem.data)
secondView.editText.setText(secondItem.data)
but after recreating activity both included views have same data (secondItem data)
whats the reason? why this problem not happened for the first time when activity created?
That's because you have multiple EditText
with the same id in your view hierarchy (included from the same file). When the Activity is recreated, it automatically restores some basic data of your views, including what text stayed in EditText, the system uses the view's id to save, find and restore the data, multiple same views with same empty ids (result by included from the same file) will mislead the restoring process
Disable your EditText automatic saving state by:
android:saveEnabled="false"
Manually save and restore state for your EditText views (optional). Or just skip this step and leave them blank:
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putString("firstValue", firstView.editText.text)
outState.putString("secondValue", secondView.editText.text)
}
override fun onRestoreInstanceState(savedInstanceState: Bundle) {
super.onRestoreInstanceState(savedInstanceState)
firstView.editText.setText(savedInstanceState.getString("firstValue"))
secondView.editText.setText(savedInstanceState.getString("secondValue"))
}