# ContextAware

Artifact: [androidx.activity:activity](https://developer.android.com/jetpack/androidx/releases/activity) [View Source](https://cs.android.com/search?q=file:androidx/activity/contextaware/ContextAware.kt+class:androidx.activity.contextaware.ContextAware) Added in [1.2.0](https://developer.android.com/jetpack/androidx/releases/activity#1.2.0)

*** ** * ** ***


```
public interface ContextAware
```

<br />

Known direct subclasses [ComponentActivity](https://developer.android.com/reference/androidx/activity/ComponentActivity), [Fragment](https://developer.android.com/reference/androidx/fragment/app/Fragment)

|---|---|
| `https://developer.android.com/reference/androidx/activity/ComponentActivity` | Base class for activities that enables composition of higher level components. |
| `https://developer.android.com/reference/androidx/fragment/app/Fragment` | Static library support version of the framework's `https://developer.android.com/reference/android/app/Fragment.html`. |

Known indirect subclasses [AbstractListDetailFragment](https://developer.android.com/reference/androidx/navigation/fragment/AbstractListDetailFragment), [AbstractProgressFragment](https://developer.android.com/reference/androidx/navigation/dynamicfeatures/fragment/ui/AbstractProgressFragment), [AmbientModeSupport](https://developer.android.com/reference/androidx/wear/ambient/AmbientModeSupport), [AppCompatActivity](https://developer.android.com/reference/androidx/appcompat/app/AppCompatActivity), [AppCompatDialogFragment](https://developer.android.com/reference/androidx/appcompat/app/AppCompatDialogFragment), [BaseCarAppActivity](https://developer.android.com/reference/androidx/car/app/activity/BaseCarAppActivity), [BaseLeanbackPreferenceFragmentCompat](https://developer.android.com/reference/androidx/leanback/preference/BaseLeanbackPreferenceFragmentCompat), [BaseSupportFragment](https://developer.android.com/reference/androidx/leanback/app/BaseSupportFragment), [BrandedSupportFragment](https://developer.android.com/reference/androidx/leanback/app/BrandedSupportFragment), [BrowseSupportFragment](https://developer.android.com/reference/androidx/leanback/app/BrowseSupportFragment), [CarAppActivity](https://developer.android.com/reference/androidx/car/app/activity/CarAppActivity), [DefaultProgressFragment](https://developer.android.com/reference/androidx/navigation/dynamicfeatures/fragment/ui/DefaultProgressFragment), [DetailsSupportFragment](https://developer.android.com/reference/androidx/leanback/app/DetailsSupportFragment), [DialogFragment](https://developer.android.com/reference/androidx/fragment/app/DialogFragment), [DynamicNavHostFragment](https://developer.android.com/reference/androidx/navigation/dynamicfeatures/fragment/DynamicNavHostFragment), [EditTextPreferenceDialogFragmentCompat](https://developer.android.com/reference/androidx/preference/EditTextPreferenceDialogFragmentCompat), [EditablePdfViewerFragment](https://developer.android.com/reference/androidx/pdf/ink/EditablePdfViewerFragment), [ErrorSupportFragment](https://developer.android.com/reference/androidx/leanback/app/ErrorSupportFragment), [FragmentActivity](https://developer.android.com/reference/androidx/fragment/app/FragmentActivity), [GuidedStepSupportFragment](https://developer.android.com/reference/androidx/leanback/app/GuidedStepSupportFragment), [HeadersSupportFragment](https://developer.android.com/reference/androidx/leanback/app/HeadersSupportFragment), [LauncherActivity](https://developer.android.com/reference/androidx/car/app/activity/LauncherActivity), [LeanbackEditTextPreferenceDialogFragmentCompat](https://developer.android.com/reference/androidx/leanback/preference/LeanbackEditTextPreferenceDialogFragmentCompat), [LeanbackListPreferenceDialogFragmentCompat](https://developer.android.com/reference/androidx/leanback/preference/LeanbackListPreferenceDialogFragmentCompat), [LeanbackPreferenceDialogFragmentCompat](https://developer.android.com/reference/androidx/leanback/preference/LeanbackPreferenceDialogFragmentCompat), [LeanbackPreferenceFragmentCompat](https://developer.android.com/reference/androidx/leanback/preference/LeanbackPreferenceFragmentCompat), [LeanbackSettingsFragmentCompat](https://developer.android.com/reference/androidx/leanback/preference/LeanbackSettingsFragmentCompat), [ListFragment](https://developer.android.com/reference/androidx/fragment/app/ListFragment), [ListPreferenceDialogFragmentCompat](https://developer.android.com/reference/androidx/preference/ListPreferenceDialogFragmentCompat), [MediaRouteChooserDialogFragment](https://developer.android.com/reference/androidx/mediarouter/app/MediaRouteChooserDialogFragment), [MediaRouteControllerDialogFragment](https://developer.android.com/reference/androidx/mediarouter/app/MediaRouteControllerDialogFragment), [MediaRouteDiscoveryFragment](https://developer.android.com/reference/androidx/mediarouter/app/MediaRouteDiscoveryFragment), [MultiSelectListPreferenceDialogFragmentCompat](https://developer.android.com/reference/androidx/preference/MultiSelectListPreferenceDialogFragmentCompat), [NavHostFragment](https://developer.android.com/reference/androidx/navigation/fragment/NavHostFragment), [OnboardingSupportFragment](https://developer.android.com/reference/androidx/leanback/app/OnboardingSupportFragment), [PdfViewerFragment](https://developer.android.com/reference/androidx/pdf/viewer/fragment/PdfViewerFragment), [PlaybackSupportFragment](https://developer.android.com/reference/androidx/leanback/app/PlaybackSupportFragment), [PreferenceDialogFragmentCompat](https://developer.android.com/reference/androidx/preference/PreferenceDialogFragmentCompat), [PreferenceFragmentCompat](https://developer.android.com/reference/androidx/preference/PreferenceFragmentCompat), [PreferenceHeaderFragmentCompat](https://developer.android.com/reference/androidx/preference/PreferenceHeaderFragmentCompat), [RowsSupportFragment](https://developer.android.com/reference/androidx/leanback/app/RowsSupportFragment), [SearchSupportFragment](https://developer.android.com/reference/androidx/leanback/app/SearchSupportFragment), [VerticalGridSupportFragment](https://developer.android.com/reference/androidx/leanback/app/VerticalGridSupportFragment), [VideoSupportFragment](https://developer.android.com/reference/androidx/leanback/app/VideoSupportFragment)

|---|---|
| `https://developer.android.com/reference/androidx/navigation/fragment/AbstractListDetailFragment` | A fragment supports adaptive two-pane layout. |
| `https://developer.android.com/reference/androidx/navigation/dynamicfeatures/fragment/ui/AbstractProgressFragment` | The base class for `https://developer.android.com/reference/androidx/fragment/app/Fragment`s that handle dynamic feature installation. |
| `https://developer.android.com/reference/androidx/wear/ambient/AmbientModeSupport` | **This class is deprecated.** Use `https://developer.android.com/reference/androidx/wear/ambient/AmbientLifecycleObserver` instead. <br /> |
| `https://developer.android.com/reference/androidx/appcompat/app/AppCompatActivity` | Base class for activities that wish to use some of the newer platform features on older Android devices. |
| `https://developer.android.com/reference/androidx/appcompat/app/AppCompatDialogFragment` | A special version of `https://developer.android.com/reference/androidx/fragment/app/DialogFragment` which uses an `https://developer.android.com/reference/androidx/appcompat/app/AppCompatDialog` in place of a platform-styled dialog. |
| `https://developer.android.com/reference/androidx/car/app/activity/BaseCarAppActivity` | Core logic for CarAppLibrary Activity interaction with a host. |
| `https://developer.android.com/reference/androidx/leanback/preference/BaseLeanbackPreferenceFragmentCompat` | This fragment provides a preference fragment with leanback-style behavior, suitable for embedding into broader UI elements. |
| `https://developer.android.com/reference/androidx/leanback/app/BaseSupportFragment` | Base class for leanback Fragments. |
| `https://developer.android.com/reference/androidx/leanback/app/BrandedSupportFragment` | Fragment class for managing search and branding using a view that implements `https://developer.android.com/reference/androidx/leanback/widget/TitleViewAdapter.Provider`. |
| `https://developer.android.com/reference/androidx/leanback/app/BrowseSupportFragment` | A fragment for creating Leanback browse screens. |
| `https://developer.android.com/reference/androidx/car/app/activity/CarAppActivity` | The class representing a car app activity in the main display. |
| `https://developer.android.com/reference/androidx/navigation/dynamicfeatures/fragment/ui/DefaultProgressFragment` | The default `https://developer.android.com/reference/androidx/fragment/app/Fragment` to display during installation progress. |
| `https://developer.android.com/reference/androidx/leanback/app/DetailsSupportFragment` | A fragment for creating Leanback details screens. |
| `https://developer.android.com/reference/androidx/fragment/app/DialogFragment` | A fragment that displays a dialog window, floating in the foreground of its activity's window. |
| `https://developer.android.com/reference/androidx/navigation/dynamicfeatures/fragment/DynamicNavHostFragment` | The `https://developer.android.com/reference/androidx/navigation/fragment/NavHostFragment` for dynamic features. |
| `https://developer.android.com/reference/androidx/preference/EditTextPreferenceDialogFragmentCompat` |   |
| `https://developer.android.com/reference/androidx/pdf/ink/EditablePdfViewerFragment` | A `https://developer.android.com/reference/androidx/fragment/app/Fragment` that extends `https://developer.android.com/reference/androidx/pdf/viewer/fragment/PdfViewerFragment` to provide PDF editing capabilities, including annotation and form filling, leveraging the 'androidx.ink' library. |
| `https://developer.android.com/reference/androidx/leanback/app/ErrorSupportFragment` | A fragment for displaying an error indication. |
| `https://developer.android.com/reference/androidx/fragment/app/FragmentActivity` | Base class for activities that want to use the support-based `https://developer.android.com/reference/androidx/fragment/app/Fragment`. |
| `https://developer.android.com/reference/androidx/leanback/app/GuidedStepSupportFragment` | A GuidedStepSupportFragment is used to guide the user through a decision or series of decisions. |
| `https://developer.android.com/reference/androidx/leanback/app/HeadersSupportFragment` | An fragment containing a list of row headers. |
| `https://developer.android.com/reference/androidx/car/app/activity/LauncherActivity` | This class handles providing the right launcher activity when running native applications and Car App Library applications. |
| `https://developer.android.com/reference/androidx/leanback/preference/LeanbackEditTextPreferenceDialogFragmentCompat` | Implemented a dialog to input text. |
| `https://developer.android.com/reference/androidx/leanback/preference/LeanbackListPreferenceDialogFragmentCompat` | Implemented a dialog to show `https://developer.android.com/reference/androidx/preference/ListPreference` or `https://developer.android.com/reference/androidx/preference/MultiSelectListPreference`. |
| `https://developer.android.com/reference/androidx/leanback/preference/LeanbackPreferenceDialogFragmentCompat` | A fragment that shows `https://developer.android.com/reference/androidx/preference/DialogPreference`, for example `https://developer.android.com/reference/androidx/preference/ListPreference` or `https://developer.android.com/reference/androidx/preference/MultiSelectListPreference`. |
| `https://developer.android.com/reference/androidx/leanback/preference/LeanbackPreferenceFragmentCompat` | This fragment provides a fully decorated leanback-style preference fragment, including a list background and header. |
| `https://developer.android.com/reference/androidx/leanback/preference/LeanbackSettingsFragmentCompat` | This fragment provides a container for displaying a `https://developer.android.com/reference/androidx/leanback/preference/LeanbackPreferenceFragmentCompat` |
| `https://developer.android.com/reference/androidx/fragment/app/ListFragment` | Static library support version of the framework's `https://developer.android.com/reference/android/app/ListFragment.html`. |
| `https://developer.android.com/reference/androidx/preference/ListPreferenceDialogFragmentCompat` |   |
| `https://developer.android.com/reference/androidx/mediarouter/app/MediaRouteChooserDialogFragment` | Media route chooser dialog fragment. |
| `https://developer.android.com/reference/androidx/mediarouter/app/MediaRouteControllerDialogFragment` | Media route controller dialog fragment. |
| `https://developer.android.com/reference/androidx/mediarouter/app/MediaRouteDiscoveryFragment` | Media route discovery fragment. |
| `https://developer.android.com/reference/androidx/preference/MultiSelectListPreferenceDialogFragmentCompat` |   |
| `https://developer.android.com/reference/androidx/navigation/fragment/NavHostFragment` | NavHostFragment provides an area within your layout for self-contained navigation to occur. |
| `https://developer.android.com/reference/androidx/leanback/app/OnboardingSupportFragment` | An OnboardingSupportFragment provides a common and simple way to build onboarding screen for applications. |
| `https://developer.android.com/reference/androidx/pdf/viewer/fragment/PdfViewerFragment` | A Fragment that renders a PDF document. |
| `https://developer.android.com/reference/androidx/leanback/app/PlaybackSupportFragment` | A fragment for displaying playback controls and related content. |
| `https://developer.android.com/reference/androidx/preference/PreferenceDialogFragmentCompat` | Abstract base class which presents a dialog associated with a `https://developer.android.com/reference/androidx/preference/DialogPreference`. |
| `https://developer.android.com/reference/androidx/preference/PreferenceFragmentCompat` | A PreferenceFragmentCompat is the entry point to using the Preference library. |
| `https://developer.android.com/reference/androidx/preference/PreferenceHeaderFragmentCompat` | `https://developer.android.com/reference/androidx/preference/PreferenceHeaderFragmentCompat` implements a two-pane fragment for preferences. |
| `https://developer.android.com/reference/androidx/leanback/app/RowsSupportFragment` | An ordered set of rows of leanback widgets. |
| `https://developer.android.com/reference/androidx/leanback/app/SearchSupportFragment` | A fragment to handle searches. |
| `https://developer.android.com/reference/androidx/leanback/app/VerticalGridSupportFragment` | A fragment for creating leanback vertical grids. |
| `https://developer.android.com/reference/androidx/leanback/app/VideoSupportFragment` | Subclass of `https://developer.android.com/reference/androidx/leanback/app/PlaybackSupportFragment` that is responsible for providing a `https://developer.android.com/reference/android/view/SurfaceView.html` and rendering video. |

*** ** * ** ***

A `ContextAware` class is associated with a `https://developer.android.com/reference/android/content/Context.html` sometime after the class is instantiated. By adding a `https://developer.android.com/reference/androidx/activity/contextaware/OnContextAvailableListener`, you can receive a callback for that event.

Classes implementing `https://developer.android.com/reference/androidx/activity/contextaware/ContextAware` are strongly recommended to also implement `https://developer.android.com/reference/androidx/lifecycle/LifecycleOwner` for providing a more general purpose API for listening for creation and destruction events.

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/activity/contextaware/ContextAwareHelper` |   |

## Summary

| ### Public methods |
|---|---|
| `abstract void` | `https://developer.android.com/reference/androidx/activity/contextaware/ContextAware#addOnContextAvailableListener(androidx.activity.contextaware.OnContextAvailableListener)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/activity/contextaware/OnContextAvailableListener listener )` Add a new `https://developer.android.com/reference/androidx/activity/contextaware/OnContextAvailableListener` for receiving a callback for when this class is associated with a `https://developer.android.com/reference/android/content/Context.html`. |
| `abstract https://developer.android.com/reference/android/content/Context.html` | `https://developer.android.com/reference/androidx/activity/contextaware/ContextAware#peekAvailableContext()()` Get the `https://developer.android.com/reference/android/content/Context.html` if it is currently available. |
| `abstract void` | `https://developer.android.com/reference/androidx/activity/contextaware/ContextAware#removeOnContextAvailableListener(androidx.activity.contextaware.OnContextAvailableListener)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/activity/contextaware/OnContextAvailableListener listener )` Remove a `https://developer.android.com/reference/androidx/activity/contextaware/OnContextAvailableListener` previously added via `https://developer.android.com/reference/androidx/activity/contextaware/ContextAware#addOnContextAvailableListener(androidx.activity.contextaware.OnContextAvailableListener)`. |

| ### Extension functions |
|---|---|
| `default final @https://developer.android.com/reference/androidx/annotation/NonNull R` | `<R extends https://developer.android.com/reference/java/lang/Object.html> https://developer.android.com/reference/androidx/activity/contextaware/ContextAwareKt.https://developer.android.com/reference/androidx/activity/contextaware/ContextAware#(androidx.activity.contextaware.ContextAware).withContextAvailable(kotlin.Function1)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/activity/contextaware/ContextAware receiver, @https://developer.android.com/reference/androidx/annotation/NonNull Function1<@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 R> onContextAvailable )` Run `https://developer.android.com/reference/androidx/activity/contextaware/package-summary#(androidx.activity.contextaware.ContextAware).withContextAvailable(kotlin.Function1)` when the `https://developer.android.com/reference/android/content/Context.html` becomes available and resume with the result. |

## Public methods

### addOnContextAvailableListener

Added in [1.2.0](https://developer.android.com/jetpack/androidx/releases/activity#1.2.0)

```
abstract void addOnContextAvailableListener(
    @NonNull OnContextAvailableListener listener
)
```

Add a new `https://developer.android.com/reference/androidx/activity/contextaware/OnContextAvailableListener` for receiving a callback for when this class is associated with a `https://developer.android.com/reference/android/content/Context.html`.

Listeners are triggered in the order they are added when added before the Context is available. Listeners added after the context has been made available will have the Context synchronously delivered to them as part of this call.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/activity/contextaware/OnContextAvailableListener listener` | The listener that should be added. |

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/activity/contextaware/ContextAware#removeOnContextAvailableListener(androidx.activity.contextaware.OnContextAvailableListener)` |   |

### peekAvailableContext

Added in [1.2.0](https://developer.android.com/jetpack/androidx/releases/activity#1.2.0)

```
abstract Context peekAvailableContext()
```

Get the `https://developer.android.com/reference/android/content/Context.html` if it is currently available. If this returns `null`, you can use `https://developer.android.com/reference/androidx/activity/contextaware/ContextAware#addOnContextAvailableListener(androidx.activity.contextaware.OnContextAvailableListener)` to receive a callback for when it available.

| Returns |
|---|---|
| `https://developer.android.com/reference/android/content/Context.html` | the Context if it is currently available. |

### removeOnContextAvailableListener

Added in [1.2.0](https://developer.android.com/jetpack/androidx/releases/activity#1.2.0)

```
abstract void removeOnContextAvailableListener(
    @NonNull OnContextAvailableListener listener
)
```

Remove a `https://developer.android.com/reference/androidx/activity/contextaware/OnContextAvailableListener` previously added via `https://developer.android.com/reference/androidx/activity/contextaware/ContextAware#addOnContextAvailableListener(androidx.activity.contextaware.OnContextAvailableListener)`.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/activity/contextaware/OnContextAvailableListener listener` | The listener that should be removed. |

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/activity/contextaware/ContextAware#addOnContextAvailableListener(androidx.activity.contextaware.OnContextAvailableListener)` |   |

## Extension functions

### ContextAwareKt.withContextAvailable

Artifact: [androidx.activity:activity](https://developer.android.com/jetpack/androidx/releases/activity) [View Source](https://cs.android.com/search?q=file:androidx/activity/contextaware/ContextAware.kt+function:withContextAvailable)

```
default final @NonNull R <R extends Object> ContextAwareKt.withContextAvailable(
    @NonNull ContextAware receiver,
    @NonNull Function1<@NonNull Context, @NonNull R> onContextAvailable
)
```

Run `https://developer.android.com/reference/androidx/activity/contextaware/package-summary#(androidx.activity.contextaware.ContextAware).withContextAvailable(kotlin.Function1)` when the `https://developer.android.com/reference/android/content/Context.html` becomes available and resume with the result.

If the `https://developer.android.com/reference/android/content/Context.html` is already available, `https://developer.android.com/reference/androidx/activity/contextaware/package-summary#(androidx.activity.contextaware.ContextAware).withContextAvailable(kotlin.Function1)` will be synchronously called on the current coroutine context. Otherwise, `https://developer.android.com/reference/androidx/activity/contextaware/package-summary#(androidx.activity.contextaware.ContextAware).withContextAvailable(kotlin.Function1)` will be called on the UI thread immediately when the Context becomes available.