ContextAwareHelper

public final class ContextAwareHelper


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

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

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

Summary

Public constructors

Construct a new ContextAwareHelper.

Public methods

@NonNull void

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

@NonNull void

Clear any Context previously made available via dispatchOnContextAvailable.

@NonNull void

Dispatch the callback of onContextAvailable to all currently added listeners in the order they were added.

@Nullable Context

Get the Context if it is currently available.

@NonNull void

Remove a OnContextAvailableListener previously added via addOnContextAvailableListener.

Public constructors

ContextAwareHelper

public ContextAwareHelper()

Construct a new ContextAwareHelper.

Public methods

addOnContextAvailableListener

@NonNull
public void addOnContextAvailableListener(
    @NonNull OnContextAvailableListener listener
)

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

Parameters
@NonNull OnContextAvailableListener listener

The listener that should be added.

See also
removeOnContextAvailableListener

#removeOnContextAvailableListener(OnContextAvailableListener)

clearAvailableContext

@NonNull
public void clearAvailableContext()

Clear any Context previously made available via dispatchOnContextAvailable.

dispatchOnContextAvailable

@NonNull
public void dispatchOnContextAvailable(@NonNull Context context)

Dispatch the callback of onContextAvailable to all currently added listeners in the order they were added.

Parameters
@NonNull Context context

The Context the ContextAware object is now associated with.

peekAvailableContext

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