# MenuProvider

# MenuProvider

Artifact:[androidx.core:core](https://developer.android.com/jetpack/androidx/releases/core)  
[View Source](https://cs.android.com/search?q=file:androidx/core/view/MenuProvider.java+class:androidx.core.view.MenuProvider)  
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/MenuProvider "View this page in Java")

<br />

```
interface MenuProvider
```

<br />

*** ** * ** ***

Interface for indicating that a component will be supplying[MenuItem](https://developer.android.com/reference/android/view/MenuItem.html)s to the component owning the app bar.

## Summary

|                                ### Public functions                                 |
|-------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)       | [onCreateMenu](https://developer.android.com/reference/kotlin/androidx/core/view/MenuProvider#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)`)` Called by the[MenuHost](https://developer.android.com/reference/kotlin/androidx/core/view/MenuHost)to allow the[MenuProvider](https://developer.android.com/reference/kotlin/androidx/core/view/MenuProvider)to inflate[MenuItem](https://developer.android.com/reference/android/view/MenuItem.html)s into the menu. |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)       | [onMenuClosed](https://developer.android.com/reference/kotlin/androidx/core/view/MenuProvider#onMenuClosed(android.view.Menu))`(menu: `[Menu](https://developer.android.com/reference/android/view/Menu.html)`)` Called by the[MenuHost](https://developer.android.com/reference/kotlin/androidx/core/view/MenuHost)when the[Menu](https://developer.android.com/reference/android/view/Menu.html)is closed.                                                                                                                                                                                                                                                             |
| [Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html) | [onMenuItemSelected](https://developer.android.com/reference/kotlin/androidx/core/view/MenuProvider#onMenuItemSelected(android.view.MenuItem))`(menuItem: `[MenuItem](https://developer.android.com/reference/android/view/MenuItem.html)`)` Called by the[MenuHost](https://developer.android.com/reference/kotlin/androidx/core/view/MenuHost)when a[MenuItem](https://developer.android.com/reference/android/view/MenuItem.html)is selected from the menu.                                                                                                                                                                                                           |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)       | [onPrepareMenu](https://developer.android.com/reference/kotlin/androidx/core/view/MenuProvider#onPrepareMenu(android.view.Menu))`(menu: `[Menu](https://developer.android.com/reference/android/view/Menu.html)`)` Called by the[MenuHost](https://developer.android.com/reference/kotlin/androidx/core/view/MenuHost)right before the[Menu](https://developer.android.com/reference/android/view/Menu.html)is shown.                                                                                                                                                                                                                                                    |

## Public functions

### onCreateMenu

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

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

Called by the[MenuHost](https://developer.android.com/reference/kotlin/androidx/core/view/MenuHost)to allow the[MenuProvider](https://developer.android.com/reference/kotlin/androidx/core/view/MenuProvider)to inflate[MenuItem](https://developer.android.com/reference/android/view/MenuItem.html)s into the menu.  

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

### onMenuClosed

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

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

Called by the[MenuHost](https://developer.android.com/reference/kotlin/androidx/core/view/MenuHost)when the[Menu](https://developer.android.com/reference/android/view/Menu.html)is closed.  

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

### onMenuItemSelected

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

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

Called by the[MenuHost](https://developer.android.com/reference/kotlin/androidx/core/view/MenuHost)when a[MenuItem](https://developer.android.com/reference/android/view/MenuItem.html)is selected from the menu.  

|                                         Parameters                                         |
|--------------------------------------------------------------------------------------------|---------------------------------|
| `menuItem: `[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`if the given menu item is handled by this menu provider,`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 by the[MenuHost](https://developer.android.com/reference/kotlin/androidx/core/view/MenuHost)right before the[Menu](https://developer.android.com/reference/android/view/Menu.html)is shown. This should be called when the menu has been dynamically updated.  

|                                   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/MenuProvider#onCreateMenu(android.view.Menu,android.view.MenuInflater)) |   |