LifecycleKey

class LifecycleKey<T : Any>(val type: KClass<T>) : ServiceKey<T, LifecycleKey.Entry<T>, LifecycleOwner, LifecycleKey.PutParams<T>>

A ServiceKey that ties the lifetime of a service instance to one or more AndroidX LifecycleOwners.

The service is created lazily, similar to a LazyKey. However, when retrieving the service, a LifecycleOwner must be provided. The service instance is retained by the locator as long as at least one of the LifecycleOwners used to retrieve it is active (i.e., its state is at or above the specified PutParams.minimumState).

Once all associated LifecycleOwners become inactive (i.e., their state drops below the minimumState), the service instance is discarded. A new instance will be created on the next request with an active LifecycleOwner.

It is an error to request a value using a LifecycleOwner that is not in at least the minimumState.

Constructors

Link copied to clipboard
constructor(type: KClass<T>)

Types

Link copied to clipboard
Link copied to clipboard
data class PutParams<T> @JvmOverloads constructor(val minimumState: Lifecycle.State = Lifecycle.State.STARTED, val threadSafetyMode: LazyThreadSafetyMode, val provider: () -> T)

Parameters required to register a LifecycleKey.

Properties

Link copied to clipboard
open override val type: KClass<T>

Functions

Link copied to clipboard
Link copied to clipboard
open override fun getValue(params: LifecycleOwner, entry: ServiceEntry<T>): T