Android Jetpack: ViewModel

ViewModel is part of the Lifecycle library which was designed to help you solve common Android Lifecycle challenges and to make your apps more maintainable and testable.

A ViewModel holds your app’s UI data in a lifecycle conscious way that survives configuration changes. Separating your app’s UI data from your Activity and Fragment classes lets you better follow the single responsibility principle: Your Activities and Fragments are responsible for drawing data to the screen, while your ViewModel can take care of holding and processing all the data needed for the UI.

Making a ViewModel is simple:
* Adding the Lifecycle library to your build.gradle file
* Extending the ViewModel class
* Use ViewModelProviders to associate your ViewModel with your UI controller

Then you can move all of your UI related data into your new ViewModel. If you need to customize ViewModel construction, you can create a ViewModelProvider.NewInstanceFactory. ViewModels also work great with the LiveData class to create reactive UIs!

A few words of warning: don’t store Contexts in ViewModels and don’t confuse ViewModels with the onSaveInstanceState method.

For more information, check out the video and links below!

Gradle dependencies →
ViewModels – A Simple Example (Good place to start for very basic usage) →
ViewModel Documentation →
ViewModels – Persistence, onSaveInstanceState(), Restoring UI State and Loaders →
Architecture Blueprint sample with Lifecycle library
* Java →
* Kotlin →
Architecture Components samples →
ViewModels and LiveData Patterns and Antipatterns →
Example of ViewModel factory in sample app →
Architecture Components landing page →
Guide to app architecture →

Watch more Android Jetpack videos here →

Subscribe to the Android Developers channel here →

#jetpack #featured

View: Android Jetpack: ViewModel

View more Technology:


33 bình luận trong “Android Jetpack: ViewModel

  1. How should I use ViewModel in a single Activity application? My app has only one activity,and multiple fragments.How can I use ViewModel in a single Activity framework? When I store Data of fragment in viewModel and get this Data on another Fragment after that use of this viewModel is finished but in Memory it store data and there is a chance to memory issue. Please suggest me in this architecture how i can use viewModel.

  2. Hey, I was wondering as per LiveData concept, if there is a change in an API Data then automatically all the ones who are observing the Livedata will be notified, so if there is a change in data from server-side would it automatically fetch the Data or we need to fetch the data and then as LiveData would change UI would change?

  3. Awesome explanation! As regards not referencing views or contexts in ViewModels, I learnt this the hard way, lol. The activity lifecycle is too unreliable to pass it into a ViewModel; app would crash with ghost references.

  4. I don't understand how can I know in onCreate() if I should use the viewmodel or the bundle? How do I know if I'm in onCreate only because of a configuration change in which case I can use the viewmodel, or because there was a lack or resources, which means the view model was destroyed and then I should use the bundle?

  5. Android is such a mess. Who cares about data being gone after rotation if your process can be killed and everything will be gone anyway. Because of that viewmodels are worthless to me. This will only make developers think that the data in viewmodels is always available

  6. @AndroidDevelopers at 1:44 you have a diagram with 5 boxes (Activity, ViewModel, Repository*, Presenter*, and more*).

    It would be really nice to see videos of the creations (or snippets) of those concepts (Repository, Presenter, and more) and why it might be good to use them.

    I have seen code in the samples but it is intermingled with everything that I lose sight of why it's important.

Trả lời

Email của bạn sẽ không được hiển thị công khai.