Jack Guo
Jack Guo

Reputation: 4714

Can't find ViewModelProviders class, only ViewModelProvider

I am trying to create lifecycle-aware view models. But I can't find ViewModelProviders class in my Android project, only ViewModelProvider. There seems to be no android.arch.lifecycle.ViewModelProviders package for me to import as well. What's happening

Upvotes: 9

Views: 4041

Answers (2)

Karan Sharma
Karan Sharma

Reputation: 2619

Also, just for future reference.

ViewModelProviders class is now deprecated. According to Android Developers Documentation: ViewModelProviders

This class is deprecated. Use the constructors for ViewModelProvider directly.

Upvotes: 4

zsmb13
zsmb13

Reputation: 89668

You probably have this dependency included in your project:

implementation "android.arch.lifecycle:viewmodel:$lifecycle_version"

That contains ViewModelProvider (and just 4 other classes), but ViewModelProviders is in a different package:

implementation "android.arch.lifecycle:extensions:$lifecycle_version"

Here are the contents of these packages for reference (as of version 1.1.1):

The contents of the extensions and viewmodel packages


For the record, you can find this out yourself by looking up the docs for the ViewModelProviders class, where it says up top:

added in version 1.1.0

belongs to Maven artifact android.arch.lifecycle:extensions:1.1.1

Upvotes: 15

Related Questions