Google is committed to advancing racial equity for Black communities. See how.

ContextAwareHelper

public final class ContextAwareHelper
extends Object

java.lang.Object
   ↳ androidx.activity.contextaware.ContextAwareHelper


Helper class for implementing ContextAware. Classes using this helper should call addOnContextAvailableListener(OnContextAvailableListener) and removeOnContextAvailableListener(OnContextAvailableListener) as the respective methods of ContextAware are called.

You must call dispatchOnContextAvailable(Context) once the Context is available to dispatch the callbacks to all registered listeners.

Listeners added after the context has been made available via dispatchOnContextAvailable(Context) will have the Context synchronously delivered to them up until clearAvailableContext() is called.

Summary

Public constructors

ContextAwareHelper()

Construct a new ContextAwareHelper.

Public methods

void addOnContextAvailableListener(OnContextAvailableListener listener)

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

void clearAvailableContext()

Clear any Context previously made available via dispatchOnContextAvailable(Context).

void dispatchOnContextAvailable(Context context)

Dispatch the callback of OnContextAvailableListener.onContextAvailable(Context) to all currently added listeners in the order they were added.

Context peekAvailableContext()

Get the Context if it is currently available.

void removeOnContextAvailableListener(OnContextAvailableListener listener)

Remove a OnContextAvailableListener previously added via addOnContextAvailableListener(OnContextAvailableListener).

Inherited methods

Public constructors

ContextAwareHelper

public ContextAwareHelper ()

Construct a new ContextAwareHelper.

Public methods

addOnContextAvailableListener

public void addOnContextAvailableListener (OnContextAvailableListener listener)

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

Parameters
listener OnContextAvailableListener: The listener that should be added.

clearAvailableContext

public void clearAvailableContext ()

Clear any Context previously made available via dispatchOnContextAvailable(Context).

dispatchOnContextAvailable

public void dispatchOnContextAvailable (Context context)

Dispatch the callback of OnContextAvailableListener.onContextAvailable(Context) to all currently added listeners in the order they were added.

Parameters
context Context: The Context the ContextAware object is now associated with.

peekAvailableContext

public Context peekAvailableContext ()

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.

Returns
Context the Context if it is currently available.

removeOnContextAvailableListener

public void removeOnContextAvailableListener (OnContextAvailableListener listener)

Remove a OnContextAvailableListener previously added via addOnContextAvailableListener(OnContextAvailableListener).

Parameters
listener OnContextAvailableListener: The listener that should be removed.