rememberViewModel

@Composable
inline fun <VM : ViewModel> rememberViewModel(tag: String? = null): ViewModelLazy<VM>

Composable function that remembers a ViewModel instance in a Composable tree using Kodein DI container. It returns ViewModelLazy, a property delegate that lazily accesses the ViewModel instance.

Return

ViewModelLazy an instance of ViewModelLazy with the specified ViewModel type.

Parameters

VM

the type of the ViewModel to be remembered.

tag

an optional tag to filter the bindings in the DI container. If changed during composition, a new instance will be created.

Throws

if no DI container is attached to the Composable tree.


@Composable
inline fun <A : Any, VM : ViewModel> rememberViewModel(tag: String? = null, arg: A): ViewModelLazy<VM>

Creates a ViewModel instance using Kodein dependency injection and remembers it for recomposition. The ViewModel will be scoped to the current scope defined by the ViewModelStoreOwner.

Return

ViewModelLazy instance representing the remembered ViewModel.

Parameters

A

The type of argument to be passed to the ViewModel constructor.

VM

The type of ViewModel to be created.

tag

The optional tag to be used for resolving ViewModel instance from DI container. If changed during composition, a new instance will be created

arg

The argument value to be passed to the ViewModel constructor. If changed during composition, a new instance will be created.

Throws

if ViewModelStoreOwner is missing for LocalViewModelStoreOwner.