onCreateOptionsMenu in subclass of AndroidHarness never called

The onCreateOptionsMenu method in a subclass of AndroidHarness is never called. I dont understand what prevents the Android system from calling it, in any other activity it is always called. Here the code which is not executed:

[java] @Override
public boolean onCreateOptionsMenu(Menu menu) {
Log.d(LOG_TAG, “onCreateOptionsMenu called”);
menu.add(“Test”); // TODO remove
return true;
}[/java]

I think thats a bug, at least would I say that it should be possible to use the menu feature

I cant find what code in the AndroidHarness class prohibits the onCreateOptionsMenu method to be called :confused:

jME is consuming the onTouch events.

Take a look at onTouch in AndroidInput.java

[EDIT] I think the onCreateOptionsMenu is probably tied to onKey instead of onTouch, but onKey is also consumed by jME. Look at onKey in AndroidInput

Ok I tried to listen to the menu key event like this:

[java]
app.getInputManager().addMapping(“Menu”, new KeyTrigger(KeyInput.KEY_LMETA));
app.getInputManager().addListener(new ActionListener() {
@Override
public void onAction(String arg0, boolean arg1, float arg2) {
System.out.println(“onMenuEvent”);
}
}, new String[] { “Menu” });[/java]

But there were no menu key events printed in the LogCat. And I dont unterstand how the Input manager would be able to prevent the AndroidHarness Activity from calling its onCreateOptionsMenu even if all key events are forwarded to the input manager?

Try using TouchInput.KEYCODE_MENU instead of KeyInput.KEY_LMETA. KeyInput is for Desktop versions while TouchInput is for Android.

If the key inputs are registered and consumed by jME, then Android won’t forward the menu key event to other things. At least that’s what I think.

Ok I got a little closer to the problem. Here is the code from the AndroidHarness class which is the problem:

[java] @Override
public void onCreate(Bundle savedInstanceState) {
logger.info(“onCreate”);
super.onCreate(savedInstanceState);

	JmeAndroidSystem.setActivity(this);
	if (screenFullScreen) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
	} else {
		if (!screenShowTitle) {
			requestWindowFeature(Window.FEATURE_NO_TITLE);
		}
	}
	...

[/java]

when calling requestWindowFeature(Window.FEATURE_NO_TITLE); the onCreateOptionsMenu method will not be called by the Android system anymore. I’m not sure if this is a feature or a bug :wink: but if I use screenFullScreen=false the onCreateOptionsMenu is now called correctly.

1 Like

Can you post a MainActivity test case that shows it working as you described above? I don’t have any projects setup right now that include android menus.

Thanks!

Ok I think i solved the problem for me the following way:

[java]
final String mappingKey = “ShowMenu”;
app.getInputManager().addMapping(mappingKey, new TouchTrigger(TouchInput.KEYCODE_MENU));
app.getInputManager().addListener(new TouchListener() {
public void onTouch(String name, TouchEvent evt, float tpf) {
switch (evt.getType()) {
case KEY_UP:
onMenuKeyPressed();
break;
}
}
}, new String[] { mappingKey });[/java]

The onCreateOptionsMenu method still is not called when the activity is initialized but as soon as onMenuKeyPressed this is done the the options menu is generated correctly. Is there a way to deactivate the handling for special keys like the menu key in the InputHandler so that Android still uses the default behavior for such key presses? That would be a nice feature, now i have to catch the keypress event and pass it back to the Android system if i want the default behavior.

1 Like
@simon.heinen said: Ok I got a little closer to the problem. Here is the code from the AndroidHarness class which is the problem:

[java] @Override
public void onCreate(Bundle savedInstanceState) {
logger.info(“onCreate”);
super.onCreate(savedInstanceState);

	JmeAndroidSystem.setActivity(this);
	if (screenFullScreen) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
	} else {
		if (!screenShowTitle) {
			requestWindowFeature(Window.FEATURE_NO_TITLE);
		}
	}
	...

[/java]

when calling requestWindowFeature(Window.FEATURE_NO_TITLE); the onCreateOptionsMenu method will not be called by the Android system anymore. I’m not sure if this is a feature or a bug :wink: but if I use screenFullScreen=false the onCreateOptionsMenu is now called correctly.

Does this mean that with screenFullScreen = false, the Android system sees the menu key and displays the menu automatically and your last post isn’t necessary? Or is your last post necessary all the time?

Also, what are you doing in onMenuKeyPressed to display the menu?

With the key listener it doesn’t matter anymore if there is an app title or not., the onMenuKeyPressed ist just one line:

[java] protected void onMenuKeyPressed() {
openOptionsMenu();
}[/java]

when openOptionsMenu is called and the android system didn’t call onCreateOptionsMenu before it will do so at that moment, so this solves my problem :slight_smile:

1 Like

I tried to set full screen to false, but the menu still wouldn’t show using the standard onCreateOptionsMenu. I had to change the engine AndroidInput onKey method to return false in order to allow the event to be seen by the Android system. IMO, we should leave the engine the way it is and have users use the standard jME InputManager to detect the menu button and call back to MainActivity to display the menu (just like you’ve done with onMenuKeyPressed).

Thanks for the tip on openOptionsMenu(). I wasn’t aware of this method.