# WebContent

Artifact: [androidx.web:web](https://developer.android.com/jetpack/androidx/releases/web) [View Source](https://cs.android.com/search?q=file:androidx/web/WebContent.kt+class:androidx.web.WebContent) Added in [1.0.0-alpha01](https://developer.android.com/jetpack/androidx/releases/web#1.0.0-alpha01)

*** ** * ** ***


```
public final class WebContent implements AutoCloseable
```

<br />

*** ** * ** ***

`https://developer.android.com/reference/androidx/web/WebContent` can `https://developer.android.com/reference/androidx/web/WebContent#attach(android.content.Context,java.util.function.Function)` and `https://developer.android.com/reference/androidx/web/WebContentView`s to outlive `https://developer.android.com/reference/android/app/Activity.html` lifetimes.

To detach the active view so that its context can be safely garbage collected, call `https://developer.android.com/reference/androidx/web/WebContent#detach()`. To permanently destroy the engine and all associated resources, call `https://developer.android.com/reference/androidx/web/WebContent#close()`.

This may only be used when `https://developer.android.com/reference/androidx/web/WebFeature#WEB_CONTENT()` feature checks pass.

## Summary

| ### Nested types |
|---|
| `public final class https://developer.android.com/reference/androidx/web/WebContent.Builder` Builder for `https://developer.android.com/reference/androidx/web/WebContent`. |

| ### Public methods |
|---|---|
| `final @https://developer.android.com/reference/androidx/annotation/NonNull T` | `@https://developer.android.com/reference/androidx/annotation/UiThread <T extends https://developer.android.com/reference/androidx/web/WebContentView> https://developer.android.com/reference/androidx/web/WebContent#attach(android.content.Context,java.util.function.Function)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/content/Context.html context, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/function/Function.html<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/content/Context.html, @https://developer.android.com/reference/androidx/annotation/NonNull T> factory )` Constructs a `https://developer.android.com/reference/androidx/web/WebContentView` bound to this `https://developer.android.com/reference/androidx/web/WebContent`. |
| `void` | `@https://developer.android.com/reference/androidx/annotation/UiThread https://developer.android.com/reference/androidx/web/WebContent#close()()` Permanently closes and destroys the underlying `https://developer.android.com/reference/androidx/web/WebContentView` engine associated with this `https://developer.android.com/reference/androidx/web/WebContent`. |
| `final void` | `@https://developer.android.com/reference/androidx/annotation/UiThread https://developer.android.com/reference/androidx/web/WebContent#detach()()` Detaches this `https://developer.android.com/reference/androidx/web/WebContent` from its current `https://developer.android.com/reference/androidx/web/WebContentView`. |

## Public methods

### attach

Added in [1.0.0-alpha01](https://developer.android.com/jetpack/androidx/releases/web#1.0.0-alpha01)

```
@UiThread
public final @NonNull T <T extends WebContentView> attach(
    @NonNull Context context,
    @NonNull Function<@NonNull Context, @NonNull T> factory
)
```

Constructs a `https://developer.android.com/reference/androidx/web/WebContentView` bound to this `https://developer.android.com/reference/androidx/web/WebContent`. This allows the underlying web engine, including its state, settings, and clients (such as `https://developer.android.com/reference/android/webkit/WebViewClient.html` and `https://developer.android.com/reference/android/webkit/WebChromeClient.html`), to outlive the view's lifetime. Previously bound `https://developer.android.com/reference/androidx/web/WebContentView` instances are destroyed when this method is called.

The provided context *must* be used to construct the `https://developer.android.com/reference/androidx/web/WebContentView`, and any previously attached `https://developer.android.com/reference/androidx/web/WebContentView` must be removed from the view hierarchy before calling this method.

`https://developer.android.com/reference/androidx/web/WebContent#detach()` must be called when this `https://developer.android.com/reference/androidx/web/WebContentView` is no longer in use to prevent context leaks. When the content is permanently retired, call `https://developer.android.com/reference/androidx/web/WebContent#close()`.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/content/Context.html context` | The context to use to construct the View. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/function/Function.html<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/content/Context.html, @https://developer.android.com/reference/androidx/annotation/NonNull T> factory` | A function that creates a new `https://developer.android.com/reference/androidx/web/WebContentView`. |

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull T` | A wrapped factory that automatically attaches this `https://developer.android.com/reference/androidx/web/WebContent` state. |

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-argument-exception/index.html` | if `https://developer.android.com/reference/androidx/web/WebContent#attach(android.content.Context,java.util.function.Function)` is not an `https://developer.android.com/reference/android/app/Activity.html` context. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-state-exception/index.html` | if a previously bound `https://developer.android.com/reference/androidx/web/WebContentView` is still in the view hierarchy, if the returned view uses a different context, or if this `https://developer.android.com/reference/androidx/web/WebContent` is closed. |

### close

Added in [1.0.0-alpha01](https://developer.android.com/jetpack/androidx/releases/web#1.0.0-alpha01)

```
@UiThread
public void close()
```

Permanently closes and destroys the underlying `https://developer.android.com/reference/androidx/web/WebContentView` engine associated with this `https://developer.android.com/reference/androidx/web/WebContent`. This method must be called from the main thread. Once closed, this `https://developer.android.com/reference/androidx/web/WebContent` instance can no longer be used.

### detach

Added in [1.0.0-alpha01](https://developer.android.com/jetpack/androidx/releases/web#1.0.0-alpha01)

```
@UiThread
public final void detach()
```

Detaches this `https://developer.android.com/reference/androidx/web/WebContent` from its current `https://developer.android.com/reference/androidx/web/WebContentView`. Any callbacks triggered will return a `https://developer.android.com/reference/androidx/web/DetachedWebContentView` in this state.

The currently attached `https://developer.android.com/reference/androidx/web/WebContentView` must be removed from the view hierarchy before this method is called.

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-state-exception/index.html` | if the current `https://developer.android.com/reference/androidx/web/WebContentView` is still in the view hierarchy. |