22

I can't get an options menu to show in a Fragment in ICS in a project which uses the android-support-v4.jar library. I'm testing on a Galaxy Nexus handset.

We aren't using the action bar, and need the app to be 2.2+ compatible. We aren't seeing any options menu in the activity in ICS (the FragmentActivity doesn't support onCreateOptionsMenu)

I can get menus working in previous version of Android - I have all the correct framework to enable the options menu (as below) but nothing shows in ICS. When stepping through the code the onCreateOptionsMenu doesn't get called. Can anyone suggest a fix?

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

public class SuperFragment extends Fragment {

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

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch(item.getItemId()) {
            case R.id.menu_sign_in:
                break;
            case R.id.menu_sign_out:
                break;
        }
        return true;
    }
    // ...
}

Target OS version in the manifest file:

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="14"/>
Martyn
  • 16,432
  • 24
  • 71
  • 104
  • Seeing your import list here would be helpful; are you using support library fragments or android.app.Fragment from the framework? Are you seeing any other menu items from the activity? Is your activity using the action bar? What's your targetSdkVersion in your manifest? – adamp Dec 01 '11 at 18:11
  • Thanks for your comment, I've added some details to my question – Martyn Dec 01 '11 at 18:17

3 Answers3

32

Removing android:targetSdkVersion="14" from the manifest enables the options menu button again.

This is because I had a theme of @android:style/Theme.Black.NoTitleBar specified in my manifest - with the android:targetSdkVersion of 14, the options menu is being inserted in to the action bar menu, as opposed to the options menu button in the button bar at the bottom of the screen and the theme is removing the activity title, and the action bar.

The action bar can be removed, although I'm not sure if this will fix the issue as I'm yet to get it working so that it's compatible across versions 2.2 - 4.

Martyn
  • 16,432
  • 24
  • 71
  • 104
  • 4
    The key is not TARGET 14, but to include it still as the maxSDK. This way ICS tries to make the old menu forwards compatible and not expecting an ActionBar – Eric Novins Dec 26 '11 at 09:24
  • 4
    Actually the key is that the target is less than 11. We found this out today. Massive headache! We thought not using the ActionBar theme would enable the compatibility menu, Obviously not! – Chris.Jenkins Mar 21 '12 at 16:26
  • What a pain! This got me going with the menu for now. Thanks! this makes the default target to 1. It may cause problems with other incompatible widgets you may have used in your app. – Ramp Feb 07 '13 at 15:29
11

Simply change the android:targetSdkVersion from "14" to "10" (less then 11), and this problem will be solved.

<uses-sdk android:minSdkVersion="3" 
      android:targetSdkVersion="10" />
Brandon
  • 111
  • 1
  • 2
2

According to the Android CDD, the cutoff for the new behavior of not showing the option menu is targetSdkVersion > 10. Your options are either to run your app in legacy mode with targetSdkVersion <= 10 or adapt your app to the new guidelines (e.g. add a menu button in your app or use an action bar). Note that you don't have to increase the target build version (project properties in eclipse) to increase the targetSdkVersion.

user1076637
  • 768
  • 5
  • 15