MenuHostHelper

Added in 1.7.0

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(onInvalidateMenuCallback: Runnable)

Construct a new MenuHostHelper.

Public functions

Unit

Adds the given MenuProvider to the helper.

Unit

Adds the given MenuProvider to the helper.

Unit
addMenuProvider(
    provider: MenuProvider,
    owner: LifecycleOwner,
    state: Lifecycle.State
)

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

Unit
onCreateMenu(menu: Menu, menuInflater: MenuInflater)

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

Unit

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.

Unit

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

Unit

Removes the given MenuProvider from the helper.

Public constructors

Added in 1.7.0
MenuHostHelper(onInvalidateMenuCallback: Runnable)

Construct a new MenuHostHelper.

Parameters
onInvalidateMenuCallback: Runnable

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

Public functions

addMenuProvider

Added in 1.7.0
fun addMenuProvider(provider: MenuProvider): Unit

Adds the given MenuProvider to the helper.

Parameters
provider: MenuProvider

the MenuProvider to be added

addMenuProvider

Added in 1.7.0
fun addMenuProvider(provider: MenuProvider, owner: LifecycleOwner): Unit

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

Parameters
provider: MenuProvider

the MenuProvider to be added

owner: LifecycleOwner

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

addMenuProvider

Added in 1.7.0
fun addMenuProvider(
    provider: MenuProvider,
    owner: LifecycleOwner,
    state: Lifecycle.State
): Unit

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
provider: MenuProvider

the MenuProvider to be added

owner: LifecycleOwner

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

state: Lifecycle.State

the Lifecycle.State to check for automated addition/removal

onCreateMenu

Added in 1.7.0
fun onCreateMenu(menu: Menu, menuInflater: MenuInflater): Unit

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

Parameters
menu: Menu

the menu to inflate all the menu items into

menuInflater: MenuInflater

the inflater to be used to inflate the menu

onMenuClosed

Added in 1.8.0
fun onMenuClosed(menu: Menu): Unit

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

Parameters
menu: Menu

the menu that has been closed

onMenuItemSelected

Added in 1.7.0
fun onMenuItemSelected(item: MenuItem): Boolean

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

Parameters
item: MenuItem

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
fun onPrepareMenu(menu: Menu): Unit

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
menu: Menu

the menu that is to be prepared

See also
onCreateMenu

removeMenuProvider

Added in 1.7.0
fun removeMenuProvider(provider: MenuProvider): Unit

Removes the given MenuProvider from the helper.

Parameters
provider: MenuProvider

the MenuProvider to be removed