With newer Android architecture components, does savedInstanceState matter?

Reading about Android new architecture components , it is recommended that you use different instances of ViewModel to pass data to Actions and Fragments.

In addition, this concept manages data from a single persistent model :

The second important principle is that you should manage your user interface with a model, preferably a persistent model. Persistence is perfect for two reasons: your users won't lose data if the OS kills your application to free up resources and your application continues to run even if the network connection is flaky or offline. Models are components that are responsible for processing data for an application. They are independent from the Views components and the applications in your application, hence they are isolated from the lifecycle concerns of those components.

And a similar concept is the only source of truth :

In this model, the database serves as the only source of truth, and other parts of the application access it through the repository. Regardless of whether you are using disk cache, we recommend that your repository designate the data source as the only source of truth for the rest of your application.

In the code examples I've seen, they pass the savedInstanceState package before the superclass is implemented, like so:

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    String userId = getArguments().getString(UID_KEY);
    viewModel = ViewModelProviders.of(this).get(UserProfileViewModel.class);
    viewModel.init(userId);
}

      

but there doesn't seem to be any reason for our actions to explicitly store / retrieve important values ​​to / from savedInstanceState.

Saved InStanceState not relevant to new architecture?

+3


source to share


1 answer


There seems to be no reason for our actions to explicitly store / retrieve important values ​​to / from savedInstanceState.

It totally depends on what is in the action.

Saved InStanceState not relevant to new architecture?

Not.

The idea behind a saved instance state was and continues to help the activity pretend as if it were around, despite the activity being destroyed and recreated along the way either due to:



  • a configuration change (such as screen rotation), or
  • process termination (for example, the user leaves the app, Android terminates the process, and the user returns to the app within half an hour)

So, for example, suppose we have an activity with a form. When the user fills out the form and clicks the "Save" item, we want to save the contents of the form. And in some cases we open a form to some existing data that is already being saved and so we want to load it. These are the problems that Android Architecture components such as Room can help with.

But suppose the user fills in a field on a form and then rotates the screen. In this case, most likely we do not want to save the changes to the database because the user did not click save to express interest in saving that change. So we rely on onSaveInstanceState()

to handle this for us. In this case, the built-in onSaveInstanceState()

handles this since the content EditText

is obvious user-volatile state.

But let's say, after editing a field, the user clicks on ImageButton

to select a contact. This is used ACTION_PICK

to call a set of contacts and the user selects a contact there. The control reverts to its original activity, which it might have TextView

to show the contact's name, or it might update ImageButton

with the contact's avatar. The user now rotates the screen. Once again, we may not want to continue with this. However, this time the activity will not be automatically held on Uri

our contact, since he does not know how to do it. So we put this in the state of the saved instance Bundle

ourselves by overriding onSaveInstanceState()

.

Data stores (such as databases) represent the only "source of truth", but it is application logic that decides when data becomes "true" and instance state is a common approach for processing data that may become true in the future but still not true.

+7


source







All Articles