# MenuHostHelper

# MenuHostHelper

Artifact:[androidx.core:core](https://developer.android.com/jetpack/androidx/releases/core)  
[View Source](https://cs.android.com/search?q=file:androidx/core/view/MenuHostHelper.java+class:androidx.core.view.MenuHostHelper)  
Added in[1.7.0](https://developer.android.com/jetpack/androidx/releases/core#1.7.0)

*** ** * ** ***

Kotlin\|[Java](https://developer.android.com/reference/androidx/core/view/MenuHostHelper "View this page in Java")

<br />

```
class MenuHostHelper
```

<br />

*** ** * ** ***

Helper class for implementing[MenuHost](https://developer.android.com/reference/kotlin/androidx/core/view/MenuHost). This class should be used to implement the[MenuHost](https://developer.android.com/reference/kotlin/androidx/core/view/MenuHost)functions. i.e.:  

```kotlin
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](https://developer.android.com/reference/kotlin/androidx/core/view/MenuHostHelper#MenuHostHelper(java.lang.Runnable))`(onInvalidateMenuCallback: `[Runnable](https://developer.android.com/reference/java/lang/Runnable.html)`)` Construct a new MenuHostHelper. |

|                                ### Public functions                                 |
|-------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)       | [addMenuProvider](https://developer.android.com/reference/kotlin/androidx/core/view/MenuHostHelper#addMenuProvider(androidx.core.view.MenuProvider))`(provider: `[MenuProvider](https://developer.android.com/reference/kotlin/androidx/core/view/MenuProvider)`)` Adds the given[MenuProvider](https://developer.android.com/reference/kotlin/androidx/core/view/MenuProvider)to the helper.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)       | [addMenuProvider](https://developer.android.com/reference/kotlin/androidx/core/view/MenuHostHelper#addMenuProvider(androidx.core.view.MenuProvider,androidx.lifecycle.LifecycleOwner))`(provider: `[MenuProvider](https://developer.android.com/reference/kotlin/androidx/core/view/MenuProvider)`, owner: `[LifecycleOwner](https://developer.android.com/reference/kotlin/androidx/lifecycle/LifecycleOwner)`)` Adds the given[MenuProvider](https://developer.android.com/reference/kotlin/androidx/core/view/MenuProvider)to the helper.                                                                                                                                                                                                                                                                                                                                                                                                    |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)       | [addMenuProvider](https://developer.android.com/reference/kotlin/androidx/core/view/MenuHostHelper#addMenuProvider(androidx.core.view.MenuProvider,androidx.lifecycle.LifecycleOwner,androidx.lifecycle.Lifecycle.State))`(` ` provider: `[MenuProvider](https://developer.android.com/reference/kotlin/androidx/core/view/MenuProvider)`,` ` owner: `[LifecycleOwner](https://developer.android.com/reference/kotlin/androidx/lifecycle/LifecycleOwner)`,` ` state: `[Lifecycle.State](https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State) `)` Adds the given[MenuProvider](https://developer.android.com/reference/kotlin/androidx/core/view/MenuProvider)to the helper once the given[LifecycleOwner](https://developer.android.com/reference/kotlin/androidx/lifecycle/LifecycleOwner)reaches the given[Lifecycle.State](https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State). |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)       | [onCreateMenu](https://developer.android.com/reference/kotlin/androidx/core/view/MenuHostHelper#onCreateMenu(android.view.Menu,android.view.MenuInflater))`(menu: `[Menu](https://developer.android.com/reference/android/view/Menu.html)`, menuInflater: `[MenuInflater](https://developer.android.com/reference/android/view/MenuInflater.html)`)` Inflates the entire[Menu](https://developer.android.com/reference/android/view/Menu.html), which will include all[MenuItem](https://developer.android.com/reference/android/view/MenuItem.html)s provided by all current[MenuProvider](https://developer.android.com/reference/kotlin/androidx/core/view/MenuProvider)s.                                                                                                                                                                                                                                                                   |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)       | [onMenuClosed](https://developer.android.com/reference/kotlin/androidx/core/view/MenuHostHelper#onMenuClosed(android.view.Menu))`(menu: `[Menu](https://developer.android.com/reference/android/view/Menu.html)`)` Called when the given[Menu](https://developer.android.com/reference/android/view/Menu.html), which was provided by one of the current[MenuProvider](https://developer.android.com/reference/kotlin/androidx/core/view/MenuProvider)s, is closed.                                                                                                                                                                                                                                                                                                                                                                                                                                                                             |
| [Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html) | [onMenuItemSelected](https://developer.android.com/reference/kotlin/androidx/core/view/MenuHostHelper#onMenuItemSelected(android.view.MenuItem))`(item: `[MenuItem](https://developer.android.com/reference/android/view/MenuItem.html)`)` Called whenever one of the menu items from any of the current[MenuProvider](https://developer.android.com/reference/kotlin/androidx/core/view/MenuProvider)s is selected.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)       | [onPrepareMenu](https://developer.android.com/reference/kotlin/androidx/core/view/MenuHostHelper#onPrepareMenu(android.view.Menu))`(menu: `[Menu](https://developer.android.com/reference/android/view/Menu.html)`)` Called right before the given[Menu](https://developer.android.com/reference/android/view/Menu.html), which was provided by one of the current[MenuProvider](https://developer.android.com/reference/kotlin/androidx/core/view/MenuProvider)s, is to be shown.                                                                                                                                                                                                                                                                                                                                                                                                                                                              |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)       | [removeMenuProvider](https://developer.android.com/reference/kotlin/androidx/core/view/MenuHostHelper#removeMenuProvider(androidx.core.view.MenuProvider))`(provider: `[MenuProvider](https://developer.android.com/reference/kotlin/androidx/core/view/MenuProvider)`)` Removes the given[MenuProvider](https://developer.android.com/reference/kotlin/androidx/core/view/MenuProvider)from the helper.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        |

## Public constructors

### MenuHostHelper

Added in[1.7.0](https://developer.android.com/jetpack/androidx/releases/core#1.7.0)  

```
MenuHostHelper(onInvalidateMenuCallback: Runnable)
```

Construct a new MenuHostHelper.  

|                                               Parameters                                                |
|---------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------|
| `onInvalidateMenuCallback: `[Runnable](https://developer.android.com/reference/java/lang/Runnable.html) | callback to invalidate the menu whenever there may be a change to it |

## Public functions

### addMenuProvider

Added in[1.7.0](https://developer.android.com/jetpack/androidx/releases/core#1.7.0)  

```
fun addMenuProvider(provider: MenuProvider): Unit
```

Adds the given[MenuProvider](https://developer.android.com/reference/kotlin/androidx/core/view/MenuProvider)to the helper.  

|                                                 Parameters                                                 |
|------------------------------------------------------------------------------------------------------------|------------------------------|
| `provider: `[MenuProvider](https://developer.android.com/reference/kotlin/androidx/core/view/MenuProvider) | the MenuProvider to be added |

### addMenuProvider

Added in[1.7.0](https://developer.android.com/jetpack/androidx/releases/core#1.7.0)  

```
fun addMenuProvider(provider: MenuProvider, owner: LifecycleOwner): Unit
```

Adds the given[MenuProvider](https://developer.android.com/reference/kotlin/androidx/core/view/MenuProvider)to the helper. This[MenuProvider](https://developer.android.com/reference/kotlin/androidx/core/view/MenuProvider)will be removed once the given[LifecycleOwner](https://developer.android.com/reference/kotlin/androidx/lifecycle/LifecycleOwner)receives an Lifecycle.Event.ON_DESTROY event.  

|                                                 Parameters                                                  |
|-------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------|
| `provider: `[MenuProvider](https://developer.android.com/reference/kotlin/androidx/core/view/MenuProvider)  | the MenuProvider to be added                                               |
| `owner: `[LifecycleOwner](https://developer.android.com/reference/kotlin/androidx/lifecycle/LifecycleOwner) | the Lifecycle owner whose state will determine the removal of the provider |

### addMenuProvider

Added in[1.7.0](https://developer.android.com/jetpack/androidx/releases/core#1.7.0)  

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

Adds the given[MenuProvider](https://developer.android.com/reference/kotlin/androidx/core/view/MenuProvider)to the helper once the given[LifecycleOwner](https://developer.android.com/reference/kotlin/androidx/lifecycle/LifecycleOwner)reaches the given[Lifecycle.State](https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State). This[MenuProvider](https://developer.android.com/reference/kotlin/androidx/core/view/MenuProvider)will be removed once the given[LifecycleOwner](https://developer.android.com/reference/kotlin/androidx/lifecycle/LifecycleOwner)goes down from the given[Lifecycle.State](https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State)or receives an Lifecycle.Event.ON_DESTROY event.  

|                                                  Parameters                                                   |
|---------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------|
| `provider: `[MenuProvider](https://developer.android.com/reference/kotlin/androidx/core/view/MenuProvider)    | the MenuProvider to be added                                               |
| `owner: `[LifecycleOwner](https://developer.android.com/reference/kotlin/androidx/lifecycle/LifecycleOwner)   | the Lifecycle owner whose state will determine the removal of the provider |
| `state: `[Lifecycle.State](https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State) | the Lifecycle.State to check for automated addition/removal                |

### onCreateMenu

Added in[1.7.0](https://developer.android.com/jetpack/androidx/releases/core#1.7.0)  

```
fun onCreateMenu(menu: Menu, menuInflater: MenuInflater): Unit
```

Inflates the entire[Menu](https://developer.android.com/reference/android/view/Menu.html), which will include all[MenuItem](https://developer.android.com/reference/android/view/MenuItem.html)s provided by all current[MenuProvider](https://developer.android.com/reference/kotlin/androidx/core/view/MenuProvider)s.  

|                                               Parameters                                               |
|--------------------------------------------------------------------------------------------------------|---------------------------------------------|
| `menu: `[Menu](https://developer.android.com/reference/android/view/Menu.html)                         | the menu to inflate all the menu items into |
| `menuInflater: `[MenuInflater](https://developer.android.com/reference/android/view/MenuInflater.html) | the inflater to be used to inflate the menu |

### onMenuClosed

Added in[1.8.0](https://developer.android.com/jetpack/androidx/releases/core#1.8.0)  

```
fun onMenuClosed(menu: Menu): Unit
```

Called when the given[Menu](https://developer.android.com/reference/android/view/Menu.html), which was provided by one of the current[MenuProvider](https://developer.android.com/reference/kotlin/androidx/core/view/MenuProvider)s, is closed.  

|                                   Parameters                                   |
|--------------------------------------------------------------------------------|-------------------------------|
| `menu: `[Menu](https://developer.android.com/reference/android/view/Menu.html) | the menu that has been closed |

### onMenuItemSelected

Added in[1.7.0](https://developer.android.com/jetpack/androidx/releases/core#1.7.0)  

```
fun onMenuItemSelected(item: MenuItem): Boolean
```

Called whenever one of the menu items from any of the current[MenuProvider](https://developer.android.com/reference/kotlin/androidx/core/view/MenuProvider)s is selected.  

|                                       Parameters                                       |
|----------------------------------------------------------------------------------------|---------------------------------|
| `item: `[MenuItem](https://developer.android.com/reference/android/view/MenuItem.html) | the menu item that was selected |

|                                       Returns                                       |
|-------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html) | `true`to indicate the menu processing was consumed by one of the[MenuProvider](https://developer.android.com/reference/kotlin/androidx/core/view/MenuProvider)s,`false`otherwise. |

### onPrepareMenu

Added in[1.8.0](https://developer.android.com/jetpack/androidx/releases/core#1.8.0)  

```
fun onPrepareMenu(menu: Menu): Unit
```

Called right before the given[Menu](https://developer.android.com/reference/android/view/Menu.html), which was provided by one of the current[MenuProvider](https://developer.android.com/reference/kotlin/androidx/core/view/MenuProvider)s, is to be shown. This happens when the menu has been dynamically modified.  

|                                   Parameters                                   |
|--------------------------------------------------------------------------------|---------------------------------|
| `menu: `[Menu](https://developer.android.com/reference/android/view/Menu.html) | the menu that is to be prepared |

|                                                                          See also                                                                          |
|------------------------------------------------------------------------------------------------------------------------------------------------------------|---|
| [onCreateMenu](https://developer.android.com/reference/kotlin/androidx/core/view/MenuHostHelper#onCreateMenu(android.view.Menu,android.view.MenuInflater)) |   |

### removeMenuProvider

Added in[1.7.0](https://developer.android.com/jetpack/androidx/releases/core#1.7.0)  

```
fun removeMenuProvider(provider: MenuProvider): Unit
```

Removes the given[MenuProvider](https://developer.android.com/reference/kotlin/androidx/core/view/MenuProvider)from the helper.  

|                                                 Parameters                                                 |
|------------------------------------------------------------------------------------------------------------|--------------------------------|
| `provider: `[MenuProvider](https://developer.android.com/reference/kotlin/androidx/core/view/MenuProvider) | the MenuProvider to be removed |