Domanda

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);
    ...
È stato utile?

Soluzione 2

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).

Altri suggerimenti

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top