MenuHostHelper

Added in 1.7.0

public class MenuHostHelper


Helper class for implementing MenuHost. This class should be used to implement the MenuHost functions. i.e.:

class ExampleComponent : MenuHost {

    private val menuHostHelper = MenuHostHelper{ invalidateMenu() }

    override fun invalidateMenu() { … }

    override fun addMenuProvider(provider: MenuProvider, owner: LifecycleOwner) {
        menuHostHelper.addMenuProvider(provider, owner)
    }

    // Override remaining MenuHost methods in similar fashion
}

Summary

Public constructors

MenuHostHelper(@NonNull Runnable onInvalidateMenuCallback)

Construct a new MenuHostHelper.

Public methods

void

Adds the given MenuProvider to the helper.

void
addMenuProvider(
    @NonNull MenuProvider provider,
    @NonNull LifecycleOwner owner
)

Adds the given MenuProvider to the helper.

void
addMenuProvider(
    @NonNull MenuProvider provider,
    @NonNull LifecycleOwner owner,
    @NonNull Lifecycle.State state
)

Adds the given MenuProvider to the helper once the given LifecycleOwner reaches the given Lifecycle.State.

void
onCreateMenu(@NonNull Menu menu, @NonNull MenuInflater menuInflater)

Inflates the entire Menu, which will include all MenuItems provided by all current MenuProviders.

void

Called when the given Menu, which was provided by one of the current MenuProviders, is closed.

boolean

Called whenever one of the menu items from any of the current MenuProviders is selected.

void

Called right before the given Menu, which was provided by one of the current MenuProviders, is to be shown.

void

Removes the given MenuProvider from the helper.

Public constructors

Added in 1.7.0
public MenuHostHelper(@NonNull Runnable onInvalidateMenuCallback)

Construct a new MenuHostHelper.

Parameters
@NonNull Runnable onInvalidateMenuCallback

callback to invalidate the menu whenever there may be a change to it

Public methods

addMenuProvider

Added in 1.7.0
public void addMenuProvider(@NonNull MenuProvider provider)

Adds the given MenuProvider to the helper.

Parameters
@NonNull MenuProvider provider

the MenuProvider to be added

addMenuProvider

Added in 1.7.0
public void addMenuProvider(
    @NonNull MenuProvider provider,
    @NonNull LifecycleOwner owner
)

Adds the given MenuProvider to the helper. This MenuProvider will be removed once the given LifecycleOwner receives an Lifecycle.Event.ON_DESTROY event.

Parameters
@NonNull MenuProvider provider

the MenuProvider to be added

@NonNull LifecycleOwner owner

the Lifecycle owner whose state will determine the removal of the provider

addMenuProvider

Added in 1.7.0
public void addMenuProvider(
    @NonNull MenuProvider provider,
    @NonNull LifecycleOwner owner,
    @NonNull Lifecycle.State state
)

Adds the given MenuProvider to the helper once the given LifecycleOwner reaches the given Lifecycle.State. This MenuProvider will be removed once the given LifecycleOwner goes down from the given Lifecycle.State or receives an Lifecycle.Event.ON_DESTROY event.

Parameters
@NonNull MenuProvider provider

the MenuProvider to be added

@NonNull LifecycleOwner owner

the Lifecycle owner whose state will determine the removal of the provider

@NonNull Lifecycle.State state

the Lifecycle.State to check for automated addition/removal

onCreateMenu

Added in 1.7.0
public void onCreateMenu(@NonNull Menu menu, @NonNull MenuInflater menuInflater)

Inflates the entire Menu, which will include all MenuItems provided by all current MenuProviders.

Parameters
@NonNull Menu menu

the menu to inflate all the menu items into

@NonNull MenuInflater menuInflater

the inflater to be used to inflate the menu

onMenuClosed

Added in 1.8.0
public void onMenuClosed(@NonNull Menu menu)

Called when the given Menu, which was provided by one of the current MenuProviders, is closed.

Parameters
@NonNull Menu menu

the menu that has been closed

onMenuItemSelected

Added in 1.7.0
public boolean onMenuItemSelected(@NonNull MenuItem item)

Called whenever one of the menu items from any of the current MenuProviders is selected.

Parameters
@NonNull MenuItem item

the menu item that was selected

Returns
boolean

true to indicate the menu processing was consumed by one of the MenuProviders, false otherwise.

onPrepareMenu

Added in 1.8.0
public void onPrepareMenu(@NonNull Menu menu)

Called right before the given Menu, which was provided by one of the current MenuProviders, is to be shown. This happens when the menu has been dynamically modified.

Parameters
@NonNull Menu menu

the menu that is to be prepared

See also
onCreateMenu

removeMenuProvider

Added in 1.7.0
public void removeMenuProvider(@NonNull MenuProvider provider)

Removes the given MenuProvider from the helper.

Parameters
@NonNull MenuProvider provider

the MenuProvider to be removed