viewModelWithSavedStateHandle

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

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

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

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 factory was found.

DI.DependencyLoopException

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


@MainThread
inline fun <F : Fragment, DIAware, VM : ViewModel> F.viewModelWithSavedStateHandle(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 factory with a SavedStateHandle argument.

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

Default scope may be overridden with parameter ownerProducer:

class MyFragment : Fragment(), DIAware {
override val di: DI by closestDI()
private val viewmodel: MyViewModel by viewModelWithSavedStateHandle(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 factory was found.

DI.DependencyLoopException

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