installOn

fun installOn(view: View, onBackPressedDispatcherOwner: OnBackPressedDispatcherOwner, findParentLifecycle: (View) -> Lifecycle = this::findParentViewTreeLifecycle)

Creates a new WorkflowLifecycleOwner and sets it on view's tags so it can be later retrieved with get.

It's very important that, once this function is called with a given view, that EITHER:

  1. The view gets attached at least once to ensure that the lifecycle eventually gets destroyed (because its parent is destroyed), or

  2. Someone eventually calls destroyOnDetach, which will either schedule the lifecycle to destroyed if the view is attached, or destroy it immediately if it's detached.

If this is not done, any observers registered with the Lifecycle may be leaked as they will never see the destroy event.

Parameters

onBackPressedDispatcherOwner

A OnBackPressedDispatcherOwner to be installed in parallel with the WorkflowLifecycleOwner, required to ensure that View.findViewTreeOnBackPressedDispatcherOwner always works. Typical use is to find one via ViewEnvironment.onBackPressedDispatcherOwner

findParentLifecycle

A function that is called whenever view is attached, and should return the Lifecycle to use as the parent lifecycle. If not specified, defaults to looking up the view tree by calling findViewTreeLifecycleOwner on view's parent, and if none is found, then looking up view's context wrapper chain for something that implements LifecycleOwner. This only needs to be passed if view will be used as the root of a new view hierarchy, e.g. for a new dialog. If no parent lifecycle is found, then the lifecycle will become RESUMED when it's attached for the first time, and stay in that state until it is re-attached with a non-null parent or destroyOnDetach is called.