Discover more from MB_Coder Newsletter
Why ViewModel shouldn't hold framework references?
ViewModel Android Context
If we want to validate data on the UI and we want to do this with a proper architecture we do this in the ViewModel or some kind of other classes we definitely don't want to do the validation logic in the UI layer. As a result, one of the best practices is to avoid performing validation logic in the UI layer.
However, most developers are aware that using string resources in Android is a best practice to make sure that you can easily translate your app to other languages and to use string resources you actually need access to the context and in ViewModel, you don't have access to the context.
The reason ViewModel shouldn't hold references:
Application Context has a different lifecycle than a ViewModel.
If the View reference remains in ViewModel and it gets destroyed and ViewModel still holds that reference it may lead to the memory leaks. Because of that developers should not pass Activity/View references to the ViewModel.