ContextAware
interface ContextAware
ComponentActivity |
Base class for activities that enables composition of higher level components. |
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 |
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.
| See also | |
|---|---|
ContextAwareHelper |
|
Summary
Public methods |
|
|---|---|
abstract @NonNull void |
Add a new |
abstract @Nullable Context |
Get the |
abstract @NonNull void |
Remove a |
Extension functions |
|
|---|---|
final @NonNull R |
<R extends Object> ContextAwareKt.withContextAvailable(Run |
Public methods
addOnContextAvailableListener
@NonNull
public 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. |
| See also | |
|---|---|
removeOnContextAvailableListener |
#removeOnContextAvailableListener(OnContextAvailableListener) |
peekAvailableContext
@Nullable
@NonNull
public 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
public abstract void removeOnContextAvailableListener(
@NonNull OnContextAvailableListener listener
)
Remove a OnContextAvailableListener previously added via addOnContextAvailableListener.
| Parameters | |
|---|---|
@NonNull OnContextAvailableListener listener |
The listener that should be removed. |
| See also | |
|---|---|
addOnContextAvailableListener |
#addOnContextAvailableListener(OnContextAvailableListener) |
Extension functions
ContextAwareKt.withContextAvailable
@NonNull
public final 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.