ViewModel

Android Architecture Components are collections of libraries that help developers design clean, robust, and maintainable applications.

Android Architecture contains classes for managing:

The ViewModel class holds and manage UI related data in lifecycle conscious way.

The ViewModel class allows data to survive configuration changes such as screen rotation.

ViewModelProvider class is used to connect UI Controller with the ViewModel.

ViewModel is also used to separate (The UI and the Data).

Never store a UI Controller or Context directly or indirectly in a ViewModel.

When developers talks about activity or a fragment, they actually referring to the UI Controller. Activity/fragment : UI Controller.

ViewModel class should be created for each UI controller in order to separate things like data and different calculations from a UI Controller.

In UI Controller there should only be necessary code for managing our UI and everything else could be placed inside the ViewModel class.

ViewModels should not hold a reference to Activities, Fragments, and Activity Contexts (Not to be confused with Application Contexts, which tied to Application LifeCycle).

If Application Contexts is needed, developers need to extend AndroidViewModel class instead, which is simly a ViewModel that includes Application reference.

Reference:

ViewModel Explained - Android Architecture Component | Tutorial
🏆 My Online Courses ⭐Discount Coupon: LAUNCH-STEVDZA-SAN https://stevdza-san.com 🐱‍👤 Wanna become a member? Join! https://www.youtube.com/channel/UCYLAirIEMMXtWOECuZAtjqQ/join 📸 Instagram https://www.instagram.com/stevdza_san/ // ViewModel dependency implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0" The ViewModel class is designed to store and manage UI-related data in a lifecycle conscious way. The ViewModel class allows data to survive configuration changes such as screen rotations.
https://www.youtube.com/watch?v=orH4K6qBzvE