ContextAware


public 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.

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 methods

abstract void

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

abstract Context

Get the Context if it is currently available.

abstract void

Remove a OnContextAvailableListener previously added via addOnContextAvailableListener.

Extension functions

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

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

Public methods

addOnContextAvailableListener

Added in 1.2.0
abstract void addOnContextAvailableListener(
    @NonNull OnContextAvailableListener listener
)

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
@NonNull OnContextAvailableListener listener

The listener that should be added.

peekAvailableContext

Added in 1.2.0
abstract Context peekAvailableContext()

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
abstract void removeOnContextAvailableListener(
    @NonNull OnContextAvailableListener listener
)

Remove a OnContextAvailableListener previously added via addOnContextAvailableListener.

Parameters
@NonNull OnContextAvailableListener listener

The listener that should be removed.

Extension functions

ContextAwareKt.withContextAvailable

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

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.