Загрузка страницы

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

Watch more Android Jetpack videos here → https://goo.gl/kw8LPv

Subscribe to the Android Developers channel here → https://goo.gl/vLYDU

#jetpack #featured

Видео Android Jetpack: ViewModel канала Android Developers
Показать
Комментарии отсутствуют
Введите заголовок:

Введите адрес ссылки:

Введите адрес видео с YouTube:

Зарегистрируйтесь или войдите с
Информация о видео
29 мая 2018 г. 21:59:53
00:05:13
Яндекс.Метрика