Blog Infos
Author
Published
Topics
Author
Published
Topics

In this article, we will learn how to use the MenuProvider API to add the Options menu to the toolbar.

Major components
  • MenuHost
  • MenuProvider
  • LifecycleOwner
  • LifecycleOwner.State
setHasOptionsMenu() , onCreateOptionsMenu(), onOptionsItemSelected() — Deprecated 🛑

 

https://developer.android.com

 

MenuProvider

It’s an interface that contains four methods

  1. onCreateMenu: Here we inflate the menu for the Options
  2. onMenuItemSelected: Here we handle what happens when the user clicks on one of the items in the Options menu
  3. onPrepareMenu: For customizing menu items, you need to put the code here.
    You can, for example, change the visibility of a view or customize the layout of menu items.
  4. onMenuClosed: Its called when the menu is closed
MenuHost
  • Interface implemented by ComponentActivity, AppCompactActivity which provides us with various methods to control the Menu added using the MenuProvider

It has the following threemethods:

// 1. It only takes 1 parameter - [MenuProvider]
/*
* We need to manually remove the MenuProvider by calling removeMenuProvider().
* */
addMenuProvider(object : MenuProvider {
override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) {
menuInflater.inflate(R.menu.main_menu, menu)
}
override fun onMenuItemSelected(menuItem: MenuItem): Boolean {
return when (menuItem.itemId) {
R.id.settings -> {
true
}
else -> false
}
}
})
// 2. It takes 2 parameters - [MenuProvider, LifeCycleOwner]
/*
* Adds the given MenuProvider to the MenuHost.
MenuProvider will be removed once the given LifecycleOwner receives an Lifecycle.Event.ON_DESTROY event.
* */
addMenuProvider(object : MenuProvider {
override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) {
menuInflater.inflate(R.menu.main_menu, menu)
}
override fun onMenuItemSelected(menuItem: MenuItem): Boolean {
return when (menuItem.itemId) {
R.id.settings -> {
Toast.makeText(this@MainActivity, "Settings...", Toast.LENGTH_SHORT).show()
true
}
else -> false
}
}
}, /*lifecycleOwner*/ this)
// 3. It takes 3 parameters - [MenuProvider, LifeCycleOwner, Lifecycle.State]
/*
* Adds a MenuProvider to the MenuHost when a given LifecycleOwner reaches a given Lifecycle.State.
LifecycleOwners will be removed once they reach a lower Lifecycle.State.
* */
addMenuProvider(object : MenuProvider {
override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) {
menuInflater.inflate(R.menu.main_menu, menu)
}
override fun onMenuItemSelected(menuItem: MenuItem): Boolean {
return when (menuItem.itemId) {
R.id.settings -> {
Toast.makeText(this@MainActivity, "Settings...", Toast.LENGTH_SHORT).show()
true
}
else -> false
}
}
}, /*lifecycleOwner*/ this, /*Lifecycle.State*/ Lifecycle.State.RESUMED)
Adding menu from Activity
addMenuProvider(object : MenuProvider {
    override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) {
        menuInflater.inflate(R.menu.main_menu, menu)
    }
    override fun onMenuItemSelected(menuItem: MenuItem): Boolean {
        return when (menuItem.itemId) {
            R.id.settings -> {
                Toast.makeText(this@MainActivity, "Settings...", Toast.LENGTH_SHORT).show()
                true
            }
            else -> false
        }
    }
})
Adding menu from Fragment

Job Offers

Job Offers


    Senior Android Developer

    SumUp
    Berlin
    • Full Time
    apply now

    Senior Android Engineer

    Carly Solutions GmbH
    Munich
    • Full Time
    apply now

OUR VIDEO RECOMMENDATION

, ,

Safe Network API Migration Tactics

This talk is about how we at Uber migrated a critical endpoint from API to gRPC stack. This talk offers a roadmap through the complexities of updating your API infrastructure.
Watch Video

Safe Network API Migration Tactics

Chris Francis & Ashok Varma
Staff Software Engineer
Uber

Safe Network API Migration Tactics

Chris Francis & As ...
Staff Software Engin ...
Uber

Safe Network API Migration Tactics

Chris Francis & ...
Staff Software Engineer
Uber

Jobs

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    // Add options menu to the toolbar
    requireActivity().addMenuProvider(object : MenuProvider {
        override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) {
            menuInflater.inflate(R.menu.main_menu, menu)
        }
        override fun onMenuItemSelected(menuItem: MenuItem): Boolean {
            return when (menuItem.itemId) {
                R.id.settings -> {
                    true
                }
                else -> false
            }
        }
    }, viewLifecycleOwner)
}

Reddit

 

This article was previously published on proandroiddev.com

YOU MAY BE INTERESTED IN

YOU MAY BE INTERESTED IN

blog
This tutorial is the second part of the series. It’ll be focussed on developing…
READ MORE
blog
We recently faced a problem with our application getting updated and reaching slowly to…
READ MORE
blog
A few weeks ago I started with a simple question — how to work…
READ MORE
blog
One of the main functions of a mobile phone was to store contacts information.…
READ MORE

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu