ContextAware

interface ContextAware

Known direct subclasses
ComponentActivity

Base class for activities that enables composition of higher level components.

Known indirect subclasses
AppCompatActivity

Base class for activities that wish to use some of the newer platform features on older Android devices.

BaseCarAppActivity

Core logic for CarAppLibrary Activity interaction with a host.

CarAppActivity

The class representing a car app activity in the main display.

FragmentActivity

Base class for activities that want to use the support-based Fragments.

LauncherActivity

This class handles providing the right launcher activity when running native applications and Car App Library applications.

PreviewActivity

Activity used to run @Composable previews from Android Studio.

SdkActivity

Activity to start for SDKs running locally.


A ContextAware class is associated with a Context sometime after the class is instantiated. By adding a OnContextAvailableListener, you can receive a callback for that event.

Classes implementing ContextAware are strongly recommended to also implement androidx.lifecycle.LifecycleOwner for providing a more general purpose API for listening for creation and destruction events.

Summary

Public functions

Unit

Add a new OnContextAvailableListener for receiving a callback for when this class is associated with a android.content.Context.

Context?

Get the Context if it is currently available.

Unit

Remove a OnContextAvailableListener previously added via addOnContextAvailableListener.

Extension functions

suspend inline R
<R : Any?> ContextAware.withContextAvailable(
    crossinline onContextAvailable: (Context) -> R
)

Run onContextAvailable when the Context becomes available and resume with the result.

Public functions

addOnContextAvailableListener

Added in 1.2.0
fun addOnContextAvailableListener(listener: OnContextAvailableListener): Unit

Add a new OnContextAvailableListener for receiving a callback for when this class is associated with a android.content.Context.

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
listener: OnContextAvailableListener

The listener that should be added.

peekAvailableContext

Added in 1.2.0
fun peekAvailableContext(): Context?

Get the Context if it is currently available. If this returns null, you can use addOnContextAvailableListener to receive a callback for when it available.

Returns
Context?

the Context if it is currently available.

removeOnContextAvailableListener

Added in 1.2.0
fun removeOnContextAvailableListener(listener: OnContextAvailableListener): Unit

Remove a OnContextAvailableListener previously added via addOnContextAvailableListener.

Parameters
listener: OnContextAvailableListener

The listener that should be removed.

Extension functions

withContextAvailable

suspend inline fun <R : Any?> ContextAware.withContextAvailable(
    crossinline onContextAvailable: (Context) -> R
): R

Run onContextAvailable when the Context becomes available and resume with the result.

If the Context is already available, onContextAvailable will be synchronously called on the current coroutine context. Otherwise, onContextAvailable will be called on the UI thread immediately when the Context becomes available.