Vitor Ferreira
Vitor Ferreira

Reputation: 1095

onOptionsItemSelected not working in fragment

I'm trying to put a menu in one of my ViewPager Fragmets. When you click on one of the options, the actions are not performed, only the menu is closed. I looked for other questions here on the StackOverFlow but they didn't help me much. How can I fix this error?

class CFragment : Fragment() {

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

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {

        // Inflate the layout for this fragment
        val view = inflater.inflate(R.layout.fragment_c, container, false)
        val toolbar = view.findViewById(R.id.toolbarCFragement) as Toolbar
        toolbar.inflateMenu(R.menu.menu_c)


        //...

return view
    }

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


    override fun onOptionsItemSelected(item: MenuItem): Boolean {

        if (item.itemId == R.id.optionOne) {
            Toast.makeText(requireActivity(), "One", Toast.LENGTH_SHORT).show()
            return true
        } else if (item.itemId == R.id.optionTwo) {
            startActivity(Intent(requireActivity(), Members::class.java))
            return true
        } else if (item.itemId == R.id.optionThree) {
            Toast.makeText(requireActivity(), "Three", Toast.LENGTH_SHORT).show()
            return true
        }else{
            return super.onOptionsItemSelected(item)
        }
    }
}

Menu:

<?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/optionOne"
        app:showAsAction="never"
        android:title="@string/optionOne">
    </item>
    <item android:id="@+id/optionTwo"
        android:title="@string/optionTwo">
    </item>
    <item android:id="@+id/optionThree"
        android:title="@string/optionThree">
    </item>
</menu>

Upvotes: 1

Views: 1242

Answers (2)

Kishan Maurya
Kishan Maurya

Reputation: 3394

As you are trying to inflate menu items in the toolbar layout then do this.

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {

        // Inflate the layout for this fragment
        val view = inflater.inflate(R.layout.fragment_c, container, false)
        val toolbar = view.findViewById(R.id.toolbarCFragement) as Toolbar
        toolbar.inflateMenu(R.menu.menu_c)
        toolbar.setOnMenuItemClickListener { item ->
            when (item.itemId) {
                R.id.optionOne -> {
                                       Toast.makeText(requireActivity(), "One", 
                                       Toast.LENGTH_SHORT).show()
                                       }
                R.id.optionTwo -> {
                                       startActivity(Intent(requireActivity(), 
                                       Members::class.java))
                                       }
              // your reset case
            }
            true
        }
      return view
    }

No need to override

onCreateOptionsMenu

Upvotes: 3

Traendy
Traendy

Reputation: 1503

I don't know your App theme or your layout files but the issue might be that you inflate your toolbar:

val toolbar = view.findViewById(R.id.toolbarCFragement) as Toolbar
toolbar.inflateMenu(R.menu.menu_c)

Further, in the code you do:

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

This inflates your menu as well. While override fun onOptionsItemSelected(item: MenuItem): Boolean does only work for the second menu, which may be under your inflated toolbar. You don't set a onMenuItemClickListener on your toolbar.

So if you want to use your toolbar that you inflate in onCreateView you have to implement:

toolbar.setOnMenuItemClickListener { /*TODO*/ }

Upvotes: 1

Related Questions