onCreateOptionsMenu is never called

拥有回忆 提交于 2019-11-28 18:32:39

If the phone you test on has a menu button onCreateOptionsMenu wont't be called on start with the theme:

android:theme="@android:style/Theme.Black.NoTitleBar"

But when you click the menu button the onCreateOptionsMenu will be called. I don't know what happens on phones without hardware buttons...

In the latest versions of Android when using the compat library for toolbar, is very common that this happens, in order to get the menu items to display in the toolbar you must do the following:

mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
noahutz

Call setHasOptionsMenu function from onCreate first. The onCreateOptionsMenu will be automatically called.

Sterling Diaz

In the method: Fragment#onCreateView(...) you should put:

setHasOptionsMenu(true);

Then your method will be called.

Danny Libin

I was having the same problem (menu not showing up, onCreateOptionsMenu not being called).

If you are calling this within a fragment, you need to override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) not public boolean onCreateOptionsMenu(Menu menu). Fragments do not use the latter, so they will never even call it.

Activity menu

Fragment menu

I had the same issue. My problem was solved by inheritance of a different activity class.

So I had been doing:

public class WelcomeActivity extends Activity

but changed this to:

public class WelcomeActivity extends AppCompatActivity

This way I was saying that say an action bar can be added to your activity.

Maybe you also have overrode onKeyDown method and made it always return true. Returning true means that keyEvent will be prevented from being propagated further. See code below:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
        /*
          handling event
         */
        return true; //onCreateOptionsMenu won't be invoked.
}

That is because our activity does not have the toolbar.

In order to add that. First, you need to add the toolbar in your activity.xml which is in res/layout

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"">

    <!-- add this part-->
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        android:theme="?attr/actionBarTheme"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

Second, let your activity append it

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

or in Kotlin

val toolbar = findViewById<Toolbar>(R.id.toolbar)
setSupportActionBar(toolbar)

I had a similar issue, but a different solution I am sharing with the community (as it took me one hour to understand what was happening):

    abstract class BaseActivity : AppCompatActivity{
      override fun onCreate(savedInstanceState: Bundle?) {
      setSupportActionBar(my_toolbar)
      }
    }
    class MyActivity : BaseActivity{
        // TODO : some good stuff
    }

where my_toolbar is an object created in my xml file through dataBinding.

The issue looks the same, no toolbar appear, no call to onCreateOptionsMenu.

My solution was to promote this code to the child class and not to that basic, as my_toolbar is only initialized as the child class is built.

Try This It works for me:---

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    super.onCreateOptionsMenu(menu);
    getMenuInflater().inflate(R.menu.home_page_menu, menu);
    return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {
    case R.id.menu_delete:
        Toast.makeText(this, "You pressed the Delete!",
                 Toast.LENGTH_LONG).show();
        break;


      case R.id.menu_setting:
     Intent intent=new Intent(HomePage.this,Setting.class);
     startActivity(intent);
     finish();
     Toast.makeText(this, "You pressed the Setting!",
     Toast.LENGTH_LONG).show(); break;


    }

    return super.onOptionsItemSelected(item);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!