Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Architecture components ViewModel vs. savedInstanceState bundle

Trying to understand what is the difference with using the ViewModel to keep some of the state of the activity or fragment, and saving them with the savedInstanceState bundle.

Got a impression that the ViewModel instance is kept alive when the activity/fragment is destroyed by os in the case like configuration change so that when os recreate the activity/fragment could get the data from the ViewModel instance which is still valid.

Does it apply to minimize the app and re-open it?

Did some test, seems minimize the app and re-open the app, the os will recreate the activity/fragment with the stavedInstanceState bundle in the onCreate() not null (whatever is saved when the onSaveInstanceStae() is called). But the ViewModel has been cleared so a new instance is created without previous ones data.

Does it it mean although is in this case the os can retrieve the saved instance state and pass to activity/fragment's onCreate(), but the ViewModel has to be a new instance without previous instance's data, or the viewModel needs do to some extra step inorder to store/restore the data cross the instances?

like image 520
lannyf Avatar asked Aug 04 '17 14:08

lannyf


People also ask

Which according to you is a better option ViewModel or savedInstanceState?

ViewModel does not shine in the process death scenario since it gets wiped away from memory along with the process and everything in it. This is where saved instance state takes center stage. But saved instance state has its own limitations.

Is ViewModel Life Cycle Aware?

Lifecycle Awareness :Viewmodel is lifecycle aware. It is automatically cleared when the lifecycle they are observing gets permanently destroyed.

Which of the following are reasons to use a ViewModel?

Which of the following are reasons to use a ViewModel? Choose as many answers as you see fit. A ViewModel and its data can survive orientation changes in an Activity/Fragment. A ViewModel allows you to separate code that updates the UI from code that doesn't need to rely on the UI or its lifecycle.

What is savedInstanceState?

The savedInstanceState is a reference to a Bundle object that is passed into the onCreate method of every Android Activity. Activities have the ability, under special circumstances, to restore themselves to a previous state using the data stored in this bundle.


1 Answers

If someone is still looking to understand difference between onSavedState vs ViewModel here is the detailed explanation:

  1. onSavedInstanceState : Primary usage of onSavedInstance was not to handle orientation change but to provide a mechanism to retrieve data if app/activity is destroyed by Android System. Example case when app is in background and Android system decide to kill this as it needs memory for some other high priority process then in this case before activity is destroyed onSavedInstanceState will be called.

  2. onSavedInstanceState only stores the Parcelable data, that provides hint to restore the state for the user when activity restarts. It saves data in System server that is a separate process.

  3. onSavedInstanceState has data limit. Only small amount of Parcelable data can be saved.

While for ViewModel

  1. ViewModel object is part of Applications process memory and hence it is able to survive configuration changes. Once a process dies, ViewModel goes away and all the saved state will be lost. Hence when activity restarts, ViewModel has nothing in it.

  2. It works as a cache for heavy objects.

  3. There are no restrictions in ViewModel.

Important: Always remember ViewModel and SavedState work together. They are not replacement or alternative to each other.

enter image description here

like image 80
TheAnkush Avatar answered Oct 05 '22 05:10

TheAnkush