https://kotlinlang.org logo
#android
Title
# android
g

ghedeon

06/06/2018, 5:13 PM
Hi, any way to pass lateinit to lazy?) probably a lot us have this code:
Copy code
@Inject
	lateinit var factory: ViewModelProvider.Factory

	val viewModel by lazy { ViewModelProviders.of(this, factory).get(FooViewModel::class.java) }
Now, I'd like to make it shorter with something like:
Copy code
val viewModel by bindViewModel<FooViewModel>(factory)

....
inline fun <reified VIEW_MODEL: ViewModel> Fragment.bindViewModel(factory: ViewModelProvider.Factory) = lazy {
	ViewModelProviders.of(this, factory)[VIEW_MODEL::class.java]
}
Which doesn't work, I assume because factory is passed before being initialized.
l

Lebohang

11/28/2019, 8:34 AM
Hi you can have something like this, hopefully it helps