TL DR: If the layout used with the data binding has EditText
, and there is a binding expression for android:text
, the binding expression overwrites the stored value of the instance state ... even if we do not explicitly call a mandatory evaluation. What the user entered before the configuration change is destroyed. How do we get around this so that when changing the configuration, the state value of the saved instance is used?
We have a stupid one Model
:
public class Model {
public String getTitle() {
return("Title");
}
}
And we have a layout that refers to Model
:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="model"
type="com.commonsware.databindingstate.Model" />
</data>
<android.support.constraint.ConstraintLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.commonsware.databindingstate.MainActivity">
<EditText android:id="@+id/title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:inputType="text"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
</layout>
Please note that this layout is not required; we will end this a bit.
The layout is used in a dynamic fragment:
public class FormFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
return(MainBinding.inflate(inflater, container, false).getRoot());
}
}
, setModel()
, Model
. MainBinding
( main.xml
, ) .
( FragmentActivity
FormFragment
) . - EditText
, , EditText
.
, android:text
:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="model"
type="com.commonsware.databindingstate.Model" />
</data>
<android.support.constraint.ConstraintLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.commonsware.databindingstate.MainActivity">
<EditText android:id="@+id/title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:inputType="text"
android:text="@{model.title}"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
</layout>
, - EditText
, EditText
. , .
, , setModel()
. , , , , setModel()
, EditText
. .
(Google Pixel, Android 8.0), (Samsung Galaxy S8, Android 7.1).
"", - . , , (, ). , , -, , .