Fragment code.
class TestFragment : Fragment() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setHasOptionsMenu(true) } override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) { inflater.inflate(R.menu.menu_test, menu); super.onCreateOptionsMenu(menu, inflater) } override fun onOptionsItemSelected(item: MenuItem): Boolean { return (when(item.itemId) { R.id.action_test -> { true } else -> super.onOptionsItemSelected(item) }) }}
Menu resource 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"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.luasoftware.test.MainActivity">
<item
android:id="@+id/action_test"
android:orderInCategory="100"
android:title="Test"
app:showAsAction="ifRoom" />
</menu>