# ViewTree

Added in 1.0.0

*** ** * ** ***


```
public final class ViewTree
```

<br />

*** ** * ** ***

## Summary

| ### Public methods |
|---|---|
| `static final https://developer.android.com/reference/android/view/ViewParent.html` | `https://developer.android.com/reference/androidx/core/viewtree/ViewTree#(android.view.View).getParentOrViewTreeDisjointParent()(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/view/View.html receiver)` Looks up a disjoint parent previously set by `https://developer.android.com/reference/androidx/core/viewtree/package-summary#(android.view.View).setViewTreeDisjointParent(android.view.ViewParent)`. |
| `static final void` | `https://developer.android.com/reference/androidx/core/viewtree/ViewTree#(android.view.View).setViewTreeDisjointParent(android.view.ViewParent)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/view/View.html receiver, https://developer.android.com/reference/android/view/ViewParent.html parent)` Assigns the disjoint parent of the given `view` to `parent`. |

## Public methods

### getParentOrViewTreeDisjointParent

Artifact: [androidx.core:core-viewtree](https://developer.android.com/jetpack/androidx/releases/core) [View Source](https://cs.android.com/search?q=file:androidx/core/viewtree/ViewTree.kt+function:getParentOrViewTreeDisjointParent) Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/core#1.0.0)

```
public static final ViewParent getParentOrViewTreeDisjointParent(@NonNull View receiver)
```

Looks up a disjoint parent previously set by `https://developer.android.com/reference/androidx/core/viewtree/package-summary#(android.view.View).setViewTreeDisjointParent(android.view.ViewParent)`.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/view/View.html receiver` | The view to get the parent or disjoint parent from |

| Returns |
|---|---|
| `https://developer.android.com/reference/android/view/ViewParent.html` | The `https://developer.android.com/reference/android/view/View.html#getParent()` or the disjoint parent of the given `view`. If present, the parent view is always preferred by this method over the disjoint parent. If the view has neither a parent nor a disjoint parent, `null` is returned. |

### setViewTreeDisjointParent

Artifact: [androidx.core:core-viewtree](https://developer.android.com/jetpack/androidx/releases/core) [View Source](https://cs.android.com/search?q=file:androidx/core/viewtree/ViewTree.kt+function:setViewTreeDisjointParent) Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/core#1.0.0)

```
public static final void setViewTreeDisjointParent(@NonNull View receiver, ViewParent parent)
```

Assigns the disjoint parent of the given `view` to `parent`.

A disjoint parent acts as an alternative to `https://developer.android.com/reference/android/view/View.html#getParent()` and is used to link two disjoint view hierarchies together. ViewOverlays, popups, and dialogs are all examples of when two view hierarchies are disjoint but related. In all of these cases, there is either a new window or new root view that has no parent of its own but conceptually is owned or otherwise related to another specific view in the disjoint hierarchy.

A disjoint parent is only used when a view has no parent of its own, so it is important to set this tag only once at the root of the tree that is disconnected from its parent. AndroidX automatically sets disjoint parents for hierarchies it creates in certain use cases, like for view overlays (used in transitions).

Setting the disjoint parent allows other components to resolve information from the disjoint parent. AndroidX will use disjoint parents to resolve values stored in the view tree, like ViewModel store owners, lifecycle owners, and more. They are not used or set by the platform. Other View-based libraries should consider setting this property to allow these and similar lookups to occur between disconnected view hierarchies. Opting in is not mandatory, and therefore disjoint parents are not guaranteed to be a comprehensive source of view ownership in all situations.

To prevent a leak, a view must not outlive its disjoint parent. Additionally, you should avoid accidentally creating a cycle in the parent and disjoint parent hierarchy. A view can be re-parented to a different disjoint parent by calling this method again in the future, and can have its disjoint parent cleared by setting it to `null`.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/view/View.html receiver` | The view to set the disjoint parent of |
| `https://developer.android.com/reference/android/view/ViewParent.html parent` | The disjoint parent to set on `view` |

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/core/viewtree/package-summary#(android.view.View).getParentOrViewTreeDisjointParent()` |   |