Architecture components: ViewModel

47 161
14.8
Следующее
Популярные
129 дней – 5 4660:50
🎨Brushes in Compose
Опубликовано 9 ноября 2017, 0:55
We've introduced Android Jetpack! View the up-to-date ViewModel video here → goo.gl/co6cFJ

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!

Learn more about LiveData and LifeCycle in this video: goo.gl/WEKghZ

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: goo.gl/nnrvKT

ViewModels - A Simple Example (Good place to start for very basic usage): goo.gl/pa2F1C
ViewModel Documentation - goo.gl/T3Jrzt
ViewModels - Persistence, onSaveInstanceState(), Restoring UI State and Loaders: goo.gl/LC1hXV
Architecture Blueprint sample with Lifecycle library - Java: goo.gl/tPsEdh , Kotlin: goo.gl/ne7mcZ
Architecture Components samples - goo.gl/KCztpb
ViewModels and LiveData - Patterns and Antipatterns: goo.gl/1t3tmL
Example of ViewModel factory in sample app - goo.gl/s7R86g
Architecture Components landing page: d.android.com/arch
Guide to app architecture: goo.gl/Vqtt34

Check out the rest of the playlist here: goo.gl/WiDD2p

Subscribe to the Android Developers channel here: goo.gl/vLYDU
автотехномузыкадетское