1

I'm trying to override the options menu in my android.support.v4.app.Fragment. The onCreateOptionsMenu method gets called when using an emulator with Android 4.0, but not if I run it on an actual device running 4.4.2.

My main Activity extends ActionBarActivity, and I make the required setHasOptionsMenu(true) call in the onCreate of my Fragment. I've tried both ActionBarCompat and ActionBarSherlock, and I have the same exact issue with both libraries.

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

    setHasOptionsMenu(true);
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    if(DEBUG) Log.v("Debug", "This method doesn't get called on a device");
    super.onCreateOptionsMenu(menu, inflater);
    ...
Sachin K
  • 339
  • 4
  • 14

2 Answers2

2

try moving setHasOptionsMenu(true); in onResume().

Rana Ranvijay Singh
  • 6,055
  • 3
  • 38
  • 54
1

I'm not sure however I'll try to help you. With ABS, I answered on a "same" behaviour and it worked well. Then, with AppCompat, you have to call super method after all your stuff as follows:

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    if(DEBUG) Log.v("Debug", "This method doesn't get called on a device");
    ... // I thought this "..." was your stuff, so do it here
    super.onCreateOptionsMenu(menu, inflater);
}  

You have a great explaination and example on this answer. You should also check your imports. However it's maybe related to your device and not with the way you do it (not sure, because on 4.2.2 with ABS, it worked well).

Community
  • 1
  • 1
Blo
  • 11,903
  • 5
  • 45
  • 99
  • 1
    Thanks! Your linked answer was very helpful. In this particular case I was running into an Exception before onCreateOptionsMenu was hit. Once that code was removed, the method was being called just fine. Still not sure why it was an issue on the 4.2.2 device but not the 4.0 emulator. – Sachin K Apr 09 '14 at 11:28