Google は、黒人コミュニティに対する人種平等の促進に取り組んでいます。取り組みを見る

ContextAware

interface ContextAware
androidx.activity.contextaware.ContextAware

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 Unit

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 Unit

Remove a OnContextAvailableListener previously added via addOnContextAvailableListener(OnContextAvailableListener).

Extension functions
From androidx.activity.contextaware
suspend R
ContextAware.withContextAvailable(crossinline onContextAvailable: (Context) -> R)

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

Public methods

addOnContextAvailableListener

abstract fun addOnContextAvailableListener(@NonNull 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

@Nullable abstract fun peekAvailableContext(): Context?

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

Return
Context? the Context if it is currently available.

removeOnContextAvailableListener

abstract fun removeOnContextAvailableListener(@NonNull listener: OnContextAvailableListener): Unit

Remove a OnContextAvailableListener previously added via addOnContextAvailableListener(OnContextAvailableListener).

Parameters
listener OnContextAvailableListener: The listener that should be removed.