简体   繁体   中英

ids on onOptionsItemSelected doesn't match

When call the method on my parent Fragment class the id from menu is different but the title is the same. I change the title in the xml from it menu to confirm and item menu is from there. I've changed menus ids, created another menu.xml and still continues. My class:

abstract class BaseFragment : Fragment() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setHasOptionsMenu(true)
    }

    override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
        super.onCreateOptionsMenu(menu, inflater)
        inflater.inflate(R.menu.header_menu, menu)
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when(item.itemId) {
            R.id.menu_company -> {
                Log.i("Company", "SELECTED")
            }
            R.id.menu_user -> {
                Log.i("USER", "SELECTED")
            }
        }
        return super.onOptionsItemSelected(item)
    }
}

My menu XML (header_menu.xml)

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/menu_user"
        android:icon="@drawable/ic_action_troca_usuario"
        android:title="User"
        app:showAsAction="always" />
    <item
        android:id="@+id/menu_company"
        android:title="Company"
        android:icon="@drawable/ic_action_seleciona_empresa"
        app:showAsAction="always"/>
</menu>

One of fragments that extends from BaseFragment:

class ControleDeVendaFragment : BaseFragment() {
...
}

In Debug mode i try to compare the ids from menu and the return is this: item.itemid = 2131230966 from selected menu company R.id.menu_company = -1000255 . i've also tried to invalidate caches and restart

I've found the problem. First i've trie clean the project and rebuild, after that, changed the menu order. Without success. And i've cleaned and rebuild again and now is working without change the code. I'm using MacOs but, don't know if it has some problem. It's not the first time on MacOs that i've to do the same thing. The same project on windows always work. Anyway. Thanks for who saw.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM