Join us for ⁠#Android11: The Beta Launch Show on June 3!

ViewTreeLifecycleOwner

open class ViewTreeLifecycleOwner
kotlin.Any
   ↳ androidx.lifecycle.ViewTreeLifecycleOwner

Accessors for finding a view tree-local LifecycleOwner that reports the lifecycle for the given view.

Summary

Public methods

open static LifecycleOwner?
get(@NonNull view: View)

Retrieve the LifecycleOwner responsible for managing the given View.

open static Unit
set(@NonNull view: View, @Nullable lifecycleOwner: LifecycleOwner?)

Set the LifecycleOwner responsible for managing the given View.

Public methods

get

@Nullable open static fun get(@NonNull view: View): LifecycleOwner?

Retrieve the LifecycleOwner responsible for managing the given View. This may be used to scope work or heavyweight resources associated with the view that may span cycles of the view becoming detached and reattached from a window.

Parameters
view View: View to fetch a LifecycleOwner for
Return
LifecycleOwner? The LifecycleOwner responsible for managing this view and/or some subset of its ancestors

set

open static fun set(
    @NonNull view: View,
    @Nullable lifecycleOwner: LifecycleOwner?
): Unit

Set the LifecycleOwner responsible for managing the given View. Calls to get(View) from this view or descendants will return lifecycleOwner.

This should only be called by constructs such as activities or fragments that manage a view tree and reflect their own lifecycle through a LifecycleOwner. Callers should only set a LifecycleOwner that will be stable. The associated lifecycle should report that it is destroyed if the view tree is removed and is not guaranteed to later become reattached to a window.

Parameters
view View: Root view managed by lifecycleOwner
lifecycleOwner LifecycleOwner?: LifecycleOwner representing the manager of the given view