ViewModelProviders is deprecated in 1.1.0
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
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
- 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
.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 courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.