# 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)

*** ** * ** ***


```
public class MenuHostHelper
```

<br />

*** ** * ** ***

Helper class for implementing `https://developer.android.com/reference/androidx/core/view/MenuHost`. This class should be used to implement the `https://developer.android.com/reference/androidx/core/view/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 |
|---|
| `https://developer.android.com/reference/androidx/core/view/MenuHostHelper#MenuHostHelper(java.lang.Runnable)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/lang/Runnable.html onInvalidateMenuCallback)` Construct a new MenuHostHelper. |

| ### Public methods |
|---|---|
| `void` | `https://developer.android.com/reference/androidx/core/view/MenuHostHelper#addMenuProvider(androidx.core.view.MenuProvider)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/core/view/MenuProvider provider)` Adds the given `https://developer.android.com/reference/androidx/core/view/MenuProvider` to the helper. |
| `void` | `https://developer.android.com/reference/androidx/core/view/MenuHostHelper#addMenuProvider(androidx.core.view.MenuProvider,androidx.lifecycle.LifecycleOwner)( @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/core/view/MenuProvider provider, @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/lifecycle/LifecycleOwner owner )` Adds the given `https://developer.android.com/reference/androidx/core/view/MenuProvider` to the helper. |
| `void` | `https://developer.android.com/reference/androidx/core/view/MenuHostHelper#addMenuProvider(androidx.core.view.MenuProvider,androidx.lifecycle.LifecycleOwner,androidx.lifecycle.Lifecycle.State)( @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/core/view/MenuProvider provider, @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/lifecycle/LifecycleOwner owner, @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/lifecycle/Lifecycle.State state )` Adds the given `https://developer.android.com/reference/androidx/core/view/MenuProvider` to the helper once the given `https://developer.android.com/reference/androidx/lifecycle/LifecycleOwner` reaches the given `https://developer.android.com/reference/androidx/lifecycle/Lifecycle.State`. |
| `void` | `https://developer.android.com/reference/androidx/core/view/MenuHostHelper#onCreateMenu(android.view.Menu,android.view.MenuInflater)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/view/Menu.html menu, @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/view/MenuInflater.html menuInflater)` Inflates the entire `https://developer.android.com/reference/android/view/Menu.html`, which will include all `https://developer.android.com/reference/android/view/MenuItem.html`s provided by all current `https://developer.android.com/reference/androidx/core/view/MenuProvider`s. |
| `void` | `https://developer.android.com/reference/androidx/core/view/MenuHostHelper#onMenuClosed(android.view.Menu)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/view/Menu.html menu)` Called when the given `https://developer.android.com/reference/android/view/Menu.html`, which was provided by one of the current `https://developer.android.com/reference/androidx/core/view/MenuProvider`s, is closed. |
| `boolean` | `https://developer.android.com/reference/androidx/core/view/MenuHostHelper#onMenuItemSelected(android.view.MenuItem)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/view/MenuItem.html item)` Called whenever one of the menu items from any of the current `https://developer.android.com/reference/androidx/core/view/MenuProvider`s is selected. |
| `void` | `https://developer.android.com/reference/androidx/core/view/MenuHostHelper#onPrepareMenu(android.view.Menu)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/view/Menu.html menu)` Called right before the given `https://developer.android.com/reference/android/view/Menu.html`, which was provided by one of the current `https://developer.android.com/reference/androidx/core/view/MenuProvider`s, is to be shown. |
| `void` | `https://developer.android.com/reference/androidx/core/view/MenuHostHelper#removeMenuProvider(androidx.core.view.MenuProvider)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/core/view/MenuProvider provider)` Removes the given `https://developer.android.com/reference/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)

```
public MenuHostHelper(@NonNull Runnable onInvalidateMenuCallback)
```

Construct a new MenuHostHelper.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/lang/Runnable.html onInvalidateMenuCallback` | callback to invalidate the menu whenever there may be a change to it |

## Public methods

### addMenuProvider

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

```
public void addMenuProvider(@NonNull MenuProvider provider)
```

Adds the given `https://developer.android.com/reference/androidx/core/view/MenuProvider` to the helper.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/core/view/MenuProvider provider` | the MenuProvider to be added |

### addMenuProvider

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

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

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

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/core/view/MenuProvider provider` | the MenuProvider to be added |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/lifecycle/LifecycleOwner owner` | 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)

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

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

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/core/view/MenuProvider provider` | the MenuProvider to be added |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/lifecycle/LifecycleOwner owner` | the Lifecycle owner whose state will determine the removal of the provider |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/lifecycle/Lifecycle.State 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)

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

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

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/view/Menu.html menu` | the menu to inflate all the menu items into |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/view/MenuInflater.html menuInflater` | 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)

```
public void onMenuClosed(@NonNull Menu menu)
```

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

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/view/Menu.html menu` | the menu that has been closed |

### onMenuItemSelected

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

```
public boolean onMenuItemSelected(@NonNull MenuItem item)
```

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

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/view/MenuItem.html item` | the menu item that was selected |

| Returns |
|---|---|
| `boolean` | `true` to indicate the menu processing was consumed by one of the `https://developer.android.com/reference/androidx/core/view/MenuProvider`s, `false` otherwise. |

### onPrepareMenu

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

```
public void onPrepareMenu(@NonNull Menu menu)
```

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

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/view/Menu.html menu` | the menu that is to be prepared |

| See also |
|---|---|
| `https://developer.android.com/reference/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)

```
public void removeMenuProvider(@NonNull MenuProvider provider)
```

Removes the given `https://developer.android.com/reference/androidx/core/view/MenuProvider` from the helper.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/core/view/MenuProvider provider` | the MenuProvider to be removed |