'Menu item won't change title

Main Code:

class MainActivity : AppCompatActivity() {
    private var optionsMenu: Menu? = null

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        menuInflater.inflate(R.menu.navigation_menu, menu)
        optionsMenu = menu
        Log.e("Options Menu", "Menu initialized")

        return super.onCreateOptionsMenu(menu)
    }

    private fun switchToNightMode() {
        SharedVars.nightMode = true
        val btnNightMode =  optionsMenu?.findItem(R.id.btnNightMode)
        btnNightMode?.title = "Light Mode"
    }
}

I made sure that the onCreateOptionsMenu is called based by a log before. Why isn't it changing when I call the switchToNightMode function?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source