How to save activity State in Android?

Before Architecture Components we were used to save the UI state by onSaveInstanceState() method

SaveInstanceState:

We need to override onSaveInstanceState(Bundle savedInstanceState) and store the application state values in the form of bundle

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
  super.onSaveInstanceState(savedInstanceState);

  savedInstanceState.putBoolean("mybool", true);
  savedInstanceState.putInt("myint", 1);
  savedInstanceState.putString("mystring", "Back to Activity State");

}

Saved instanceState could save only parceble data.
This have limit to staore small amount of parceble data

ViewModel

ViewModel instance is part of Applications process  and hence it is able to handle configuration changes.
If process dies, ViewModel instance lost and all the saved state will also be lost. 

It will work for large objects also.
Don't have any restritions to save the state.
You can read about Viewmodel here


Subscribe For Daily Updates

Flutter Questions
Android Questions