简体   繁体   中英

the app doesn't enter the onCreateOptionsMenu(Menu menu) override function

I am trying to add a menu to one of my app's pages, but for some reason, it doesn't appear. I made this specific page extend AppCompactActivity and I override both onCreateOptionsMenu(Menu menu) and onOptionsItemSelected(MenuItem item). But, when I go to the page it's like both of this functions were never created. There is no problem shown in the Logcat and the app runs smoothly, but it's like the app doesn't enter into the onCreateOptionsMenu(Menu menu) function (I tried to make a toast of break a line inside the function but nothing happened...). Does anyone have an idea what may be the problem?

The java code

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    Toast.makeText(this, "check", Toast.LENGTH_LONG).show();
    getMenuInflater().inflate(R.menu.menu_edit_sound, menu);
    return true;
}

The menu

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<item android:id="@+id/menu_back"
    android:title="back"
    app:showAsAction="ifRoom"
    android:orderInCategory="5"/>

<item android:id="@+id/menu_save"
    android:title="save"
    app:showAsAction="ifRoom"
    android:orderInCategory="10"/>

In the onCreate of your fragment, call setHasOptionsMenu(true) in order for menu lifecycle methods to be called.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
}

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