2

onCreateOptionsMenu does not get called in a fragment. I have to manually call setHasOptionsMenu(true) in onCreatedView() but this causes the item.itemId be an empty String "" in the onOptionsItemSelected() and therefore i can't detect which menu item was tapped.

I'm currently using this in a fragment:

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

override fun onOptionsItemSelected(item: MenuItem): Boolean {
    when (item.itemId) {
        R.id.menu_font_size_Btn -> {

        }
        R.id.menu_share_Btn -> {
            super.showShareSheet()
        }
    }

    return super.onOptionsItemSelected(item)
}
SwiftiSwift
  • 7,528
  • 9
  • 56
  • 96

4 Answers4

3

Call super.onCreateOptionsMenu(menu,inflater) after menu inflate

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

This may more help Ref :: https://stackoverflow.com/a/15654039/11393354

try this,

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_sample, menu);
    super.onCreateOptionsMenu(menu,inflater);
}

And in onCreate add this line to make the options appear in your Toolbar

setHasOptionsMenu(true);
Joker
  • 153
  • 14
2

To make onCreateOptionsMenu work inside your fragment you can follow the below steps:

Be sure to set your Toolbar as ActionBar (if using one):

styles.xml

<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
    ...
</style>

In your Activity's onCreate:

setSupportActionBar(my_toolbar)

Without this line, onOptionsItemSelected event won't run inside your fragment. This assigns all the ActionBar callbacks to Toolbar.

In your fragments onViewCreated or onCreateView :

setHasOptionsMenu(true)

And also override this method:

override fun onOptionsItemSelected(item: MenuItem): Boolean {
    when (item.id) {
        R.id.item_01 -> {}
        R.id.item_02 -> {}
        ...
    }
    super.onCreateOptionsMenu(menu, inflater)
}

If you are using Android Navigation Component add this in your Activity:

setupActionBarWithNavController(navController)
Arrowsome
  • 2,649
  • 3
  • 10
  • 35
1

Fragment-owned app bar: In case you have your Toolbar defined in your Fragment layout and not in the layout of the parent Activity, you should use the Toolbar APIs to inflate the menu. (Fragment menu APIs are appropriate only for activity-owned app bars)

Fragment Layout

<androidx.appcompat.widget.Toolbar
    android:id="@+id/toolbar"
    ... />

Fragment

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {

    ...
    val toolbar = binding.toolbar
    toolbar.inflateMenu(R.menu.sample_menu)
    toolbar.setOnMenuItemClickListener {
            when (it.itemId) {
                R.id.action_settings -> {
                    // Navigate to settings screen
                    true
                }
                R.id.action_done -> {
                    // Save profile changes
                    true
                }
                else -> false
            }
        }

    //set item visibility
    toolbar.menu.findItem(R.id.action_done).isVisible = ...
    ...
}
Aegir
  • 899
  • 8
  • 19
0
 override fun onCreateOptionsMenu(menu: Menu?): Boolean {
    menuInflater.inflate(R.menu.menu, menu)
    return true
}

 override fun onOptionsItemSelected(item: MenuItem): Boolean {
    // Handle item selection
    return when (item.getItemId()) {
        R.id. menu_font_size_Btn -> {
           //your TODO
            true
        }
        else -> super.onOptionsItemSelected(item)
    }
   }
sasikumar
  • 12,540
  • 3
  • 28
  • 48