0

I have a little problem I want show a menu in my action bar when own_product is true but when i change the value of my boolean the menu it still not visible, the boolean var change later.

private var own_product = false    

override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        println(own_product)
        return if(own_product){
            menuInflater.inflate(R.menu.product_details_menu,menu)
            true
        } else false
    }
fun myfunction(){
 productViewModel.getProductById(id).observe(this, Observer {

            product = it
            if(product.user_id == auth.currentUser!!.email){
                own_product = true
            }
            println(own_product)
            title.text = product.title
            description.text = product.description
            date.text = product.date.toString()
            image.setImageBitmap(product.image)
            price.text = product.price.toString() +"€"
}


How can I solve this problem?

Gsliii
  • 3
  • 3
  • Try calling `super.onCreateOptionsMenu` before you call `menuInflater` also make sure you have `setHasOptionMenu(true)` in your `onCreate`. Source: [onCreateOptionsMenu not getting called - Bob Snyder](https://stackoverflow.com/a/35054179/16653700). – Alias Cartellano Dec 15 '21 at 17:12

1 Answers1

0

To refresh the menu you should use below code after your boolean changes

  1. Fragment

    requireActivity().invalidateOptionsMenu()

  2. Activity

    invalidateOptionsMenu()

This would notify android that contents of menu has changed and it will redraw the menu with new value