ViewModelLifecycle

class ViewModelLifecycle(provider: ViewModelLifecycleOwner, isEnforceMainThread: Boolean) : Lifecycle

Author

skydoves (Jaewoong Eum)

An implementation of Lifecycle, which follows the ViewModel's lifecycle. ViewModelLifecycle handles multiple LifecycleObserver and ViewModelLifecycleObserver to track ViewModel's lifecycle. ViewModelLifecycle belongs to ViewModelLifecycleOwner, you can get this directly from the ViewModelLifecycleOwner.

ViewModelLifecycleOwner only handles Lifecycle.Event.ON_START and Lifecycle.Event.ON_DESTROY, which are the same as the ViewModelState.INITIALIZED and ViewModelState.CLEARED.

Constructors

Link copied to clipboard
fun ViewModelLifecycle(provider: ViewModelLifecycleOwner, isEnforceMainThread: Boolean = true)

Functions

Link copied to clipboard
open override fun addObserver(observer: LifecycleObserver)

Adds a LifecycleObserver that will be notified when the ViewModelLifecycle changes state.

Link copied to clipboard
fun addViewModelLifecycleObserver(onStateChanged: (viewModelState: ViewModelState) -> Unit)
fun addViewModelLifecycleObserver(onInitialized: (owner: LifecycleOwner) -> Unit, onCleared: (owner: LifecycleOwner) -> Unit)

Adds a ViewModelLifecycleObserver that will be notified when the ViewModelLifecycle changes state.

Link copied to clipboard
open override fun getCurrentState(): Lifecycle.State

Returns the current state of the ViewModel lifecycle. ViewModelLifecycle has only two lifecycle states; Lifecycle.Event.ON_START and Lifecycle.Event.ON_DESTROY, so make sure don't depend other lifecycle states.

Link copied to clipboard
fun getObserverCount(): Int

Returns the current count of the observers.

Link copied to clipboard
open override fun removeObserver(observer: LifecycleObserver)

Remove a LifecycleObserver from the internal observers list.

Properties

Link copied to clipboard
val isCleared: Boolean

Check the current ViewModelState is cleared or not.

Link copied to clipboard

Extensions

Link copied to clipboard
fun ViewModelLifecycle.addViewModelOnClearedObserver(onCleared: (owner: LifecycleOwner) -> Unit)
Link copied to clipboard
fun ViewModelLifecycle.addViewModelOnInitializedObserver(onInitialized: (owner: LifecycleOwner) -> Unit)