Kai R.
Kai R.

Reputation: 185

Android onCreateOptionsMenu not called when hitting the menu button

I have the problem that my application doesn't show the options menu when I hit the menu button. Debugging shows that the onCreateOptionsMenu(Menu menu) method is not called after hitting the menu button. I have another application with the same code for the menu and there it works. So now my code:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.app_menu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {
        case R.id.options:
            Intent intent = new Intent(this, OptionsActivity.class);
            startActivityForResult(intent, 1);
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }

}

In res -> menu -> app_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
   <item android:id="@+id/options" android:title="@string/options" /> 
</menu>

I have no idea why the onCreateOptionsMenu is not called after hitting the menu button. I hope you guys can help me.

Edit: I'm not using Fragments and the onCreateOptionsMenu is really never called. Not at the start of the app and not when i'm hitting the menu button on my device.

Upvotes: 2

Views: 2843

Answers (4)

Haider Abbas
Haider Abbas

Reputation: 33

In Manifest file just set target versions as below: android:minSdkVersion="8" android:targetSdkVersion="10"

Upvotes: -1

Renjith
Renjith

Reputation: 3617

Not sure from your post that you're using Fragments. If so, you must set menu options on by

setHasOptionMenu(true);

call this method from Fragment's onCreate() and the options menu will then be shown.

Upvotes: 2

SHASHIDHAR MANCHUKONDA
SHASHIDHAR MANCHUKONDA

Reputation: 3322

It will call when app starts for the first time not after menu item selected.

@Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
if (item.getItemId() == R.id.menuitem_id) {
}
return super.onMenuItemSelected(featureId, item);
}

this method will be called after selection

Upvotes: 1

emen
emen

Reputation: 6308

Try to add these item attributes in your app_menu.xml file:

<item
    android:id="@+id/options"
    android:orderInCategory="100"
    android:showAsAction="never"
    android:title="@string/options"/>

You must declare a string for options in Strings.xml

Upvotes: 1

Related Questions