How to save user inputs when changing screen orientation using Android DataBinding library?

I am at the very beginning of a new Android project. After playing with MVP in my last project, I want to implement MVVM with data binding this time.

I have a problem handling the DataBinding correctly when it comes to configuration changes, such as changing the orientation of the screen.

All DataBinding samples (everything that I found when searching for β€œandroid mvvm data binding”) have the same problem: When I enter something into EditText and rotate the screen, then EditText is empty.

Once in my layout there is something like the following: I can’t get the views (EditText in this case) to restore their state after changing the rotation of the screen.

<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <data>
        <variable
            name="vm"
            type="com.example.app.TestViewModel" />
    </data>

    <EditText android:id="@+id/question"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@={vm.question}" 
        android:hint="Question" />

</layout>

, - onCreate .

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ActivityTestBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_test);
    binding.setVm(new TestViewModel());
}

?

, .

+3
3

ViewModels .

:

public class TestViewModel extends ViewModel{
    ...
}

Activity#onCreate(...):

public class MainActivity extends LifecycleActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityTestBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_test);

        ViewModelProvider provider = ViewModelProviders.of(this);
        binding.setVm(provider.get(TestViewModel.class));
    }
}
-2

, .

, . , , .

, executePendingBindings() , , Activity.onCreate():

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ActivityTestBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_test);

    viewModel = new TestViewModel();
    binding.setVm(viewModel);
    binding.executePendingBindings();
}

, . , android:id, .

, , ViewModel .

+9

You need to save the state of the model of your view by overriding the method onSaveInstanceState()in your activity class and restoring it in the method onCreate().

private static final QUESTION = "testViewModel.question";
private TestViewModel mTestViewModel;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ActivityTestBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_test);

    mTestViewModel = new TestViewModel();
    // Check whether we're recreating a previously destroyed instance
    if (savedInstanceState != null) {
         // restore view model state
         String questionVal = savedInstanceState.getString(QUESTION, "");
         mTestViewModel.setQuestion(questionVal);
    }
    binding.setVm(mTestViewModel);
}

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    // Save current view model state
    savedInstanceState.putString(QUESTION, mTestViewModel.getQuestion());

    // Always call the superclass so it can save the view hierarchy state
    super.onSaveInstanceState(savedInstanceState);
}

Read more about the "save-restore" technology, which you can read in this part of the documentation.

+3
source

Source: https://habr.com/ru/post/1687160/


All Articles