ViewModelProviders is deprecated in 1.1.0
Codemia Knowledge Hub ·
Introduction
ViewModelProviders.of() was deprecated in AndroidX Lifecycle version 1.1.0 and removed in later versions. The replacement is ViewModelProvider(owner) constructor or the Kotlin by viewModels() delegate. The migration is straightforward — replace ViewModelProviders.of(this).get(MyViewModel::class.java) with ViewModelProvider(this).get(MyViewModel::class.java) or use by viewModels() for the cleanest Kotlin syntax.
The Deprecated Code
Fix 1: ViewModelProvider Constructor (Java & Kotlin)
Java equivalent:
Fix 2: by viewModels() Delegate (Kotlin — Recommended)
The cleanest approach using Kotlin property delegates:
With Custom ViewModelFactory
When your ViewModel needs constructor parameters:
Sharing ViewModel Between Fragments
Gradle Dependencies
Migration Checklist
Common Pitfalls
- Missing
activity-ktxorfragment-ktxdependency: Theby viewModels()delegate is an extension function inactivity-ktxandfragment-ktx. Without the dependency, the compiler reports "Unresolved reference: viewModels". - Using
by viewModels()vsby activityViewModels():by viewModels()in a Fragment creates a ViewModel scoped to that fragment. To share state between fragments, useby activityViewModels()which scopes the ViewModel to the parent activity. - Accessing ViewModel before
onCreate:by viewModels()is lazy — it creates the ViewModel on first access. Accessing it beforeonCreate(e.g., in property initializers) causes a crash because theViewModelStoreis not yet available. ViewModelProvidersclass not found at compile time: If you updated the lifecycle dependency but still referenceViewModelProviders, the class is not in the new artifact. Remove all references and replace withViewModelProvider.- Custom factory with
by viewModels(): The lambda inby viewModels { factory }must return aViewModelProvider.Factory. Returning the ViewModel directly causes a type mismatch error.
Summary
- Replace
ViewModelProviders.of(this).get(VM::class.java)withViewModelProvider(this).get(VM::class.java) - Use
by viewModels()Kotlin delegate for the cleanest, lazily-initialized syntax - Use
by activityViewModels()to share a ViewModel between fragments - Add
activity-ktxandfragment-ktxdependencies for the delegate extensions - Pass a
ViewModelProvider.Factoryfor ViewModels that need constructor parameters
Related reading
- ViewPager and fragments — what's the right way to store fragment's state?
- viewpager setonpagechangelistener deprecated
- View's getWidth() and getHeight() returns 0
- viewWillDisappear Determine whether view controller is being popped or is showing a sub-view controller
- Visual List of iOS Fonts?
- Visual Studio Code - Target of URI doesn't exist 'packageflutter/material.dart
- Wait until swift for loop with asynchronous network requests finishes executing
- Waiting for asynchronous callback in Android's IntentService
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.