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.

CarAppActivity

The class representing a car app activity.

FragmentActivity

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


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 @NonNull void

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

abstract @Nullable Context

Get the Context if it is currently available.

abstract @NonNull void

Remove a OnContextAvailableListener previously added via addOnContextAvailableListener.

Extension functions

final default @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

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

@Nullable
@NonNull
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

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

@NonNull
final default 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, o