ViewModelFactory

A ViewModelProvider will use a ViewModelProvider.Factory to create instances of your ViewModel subclass. If you have no constructor parameters — or if you are extending AndroidViewModel and just have the Application constructor parameter — then the default ViewModelProvider.Factory will suffice. No special configuration is necessary.

However, there will be times when you will want to pass other constructor parameters to your ViewModel subclass. You can have those parameters, but now the Jetpack does not know how to create instances of your ViewModel. You will need to supply the ViewModelProvider a ViewModelProvider.Factory that knows how to create those instances.


Prev Table of Contents Next

This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.