viewModel

@MainThread
inline fun <A : AppCompatActivity, DIAware, VM : ViewModel> A.viewModel(tag: Any? = null): Lazy<VM>

Returns a Lazy property delegate to access the AppCompatActivity's ViewModel, which will be obtained from a Kodein DI provider.

class MyAppCompatActivity : AppCompatActivity(), DIAware {
override val di: DI by closestDI()
private val viewmodel: MyViewModel by viewModel()
}

Parameters

A

Extension function applies to AppCompatActivity which implements DIAware

VM

The type of ViewModel to obtain

tag

The bound Kodein DI tag

Throws

if accessed before the AppCompatActivity is attached to the Application

DI.NotFoundException

If no provider was found.

DI.DependencyLoopException

When calling the provider, if the value construction triggered a dependency loop.


@MainThread
inline fun <F : Fragment, DIAware, VM : ViewModel> F.viewModel(noinline ownerProducer: () -> ViewModelStoreOwner = { this }, tag: Any? = null): Lazy<VM>

Returns a Lazy property delegate to access a ViewModel by default scoped to this Fragment, which will be obtained from a Kodein DI provider.

class MyFragment : Fragment(), DIAware {
override val di: DI by closesDI()
private val viewmodel: MyViewModel by viewModel()
}

Default scope may be overridden with parameter ownerProducer:

class MyFragment : Fragment(), DIAware {
override val di: DI by closestDI()
private val viewmodel: MyViewModel by viewModel (ownerProducer = { requireParentFragment() })
}

Parameters

F

Extension function applies to Fragment which implements DIAware

VM

The type of ViewModel to obtain

ownerProducer

Optionally override the default ViewModel owner

tag

The bound Kodein DI tag

Throws

if accessed before this Fragment is attached

DI.NotFoundException

If no provider was found.

DI.DependencyLoopException

When calling the provider, if the value construction triggered a dependency loop.