I've tried the new way to construct the viewmodels...
# android
t
I've tried the new way to construct the viewmodels in activities according to the [docs](https://developer.android.com/jetpack/androidx/releases/lifecycle#lifecycle-viewmodel-compose-2.5.0-alpha01), it feels good. But I didn't find a way to provide the
defaultViewModelCreationExtras
in compose, did I miss something?
i
You don't control the default extras for a
NavBackStackEntry
(just like how you don't control the default factory either). While it didn't make it into this alpha01 release, expect two approaches going forward: 1.
viewModel()
that takes
CreationExtras
, in addition to the factory (thus allowing you to provide your own
CreationExtras
to your custom factory) 2. A new
viewModel()
overload that takes a lambda method to construct the ViewModel:
Copy code
val viewModel: HomeViewModel = viewModel {
  // Directly reference any value without having
  // to worry about a Factory or CreationExtras
  HomeViewModel(valueFromComposition, repository)
}
You can technically write those both yourself right now (as the underlying APIs that you need are already there)
t
👍
c
So this paves the way for "AssistedInject" I suppose?
i
Yep, the lambda based approach lets you use any variable as part of your construction