This should be a simple question, but I just can’t seem to figure it out. I’m trying to capture the event of the user hitting the menu button from the main android activity that extends AndroidHarness. I know that I should be avoiding this to keep things as platform independant as possible, but since Nifty is slowing things down so much on android, I need to create a couple of android activities for setting game preferences.
Is there a way to see the inputManager from the android activity? app.getInputManager() is returning null when I try to use it in the onCreate of the activity.
[java]
MainActivity extends AndroidHarness
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// app.getInputManager() returns null here
}
[/java]
I’m sure I’ll get a slap on the wrist for being naughty, but I wanted to do a similar thing - I wanted to bypass the quit dialog when they pressed the back key, so I hope this will help you. Its from the Activity that extends AndroidHarness: -
(I have a Boolean declared further up called showExitDialog)
[java] final private String ESCAPE_EVENT = “TouchEscape”; //had to c/p from the source
@Override
public void onTouch(String name, TouchEvent evt, float tpf)
//stolen from AndroidHarness, just me testing modding the Andoid interface
{
if (name.equals(ESCAPE_EVENT))
{
switch(evt.getType())
{
case KEY_UP:
this.runOnUiThread(new Runnable() {
@Override
public void run() {
if (!showExitDialog) {
if (app != null)
app.stop(true);
MainActivity.this.finish();
} else {
AlertDialog dialog = new AlertDialog.Builder(MainActivity.this)
// .setIcon(R.drawable.alert_dialog_icon)
.setTitle(exitDialogTitle)
.setPositiveButton(“Yes”, MainActivity.this)
.setNegativeButton(“No”, MainActivity.this)
.setMessage(exitDialogMessage)
.create();
dialog.show();
}
}
});
break;
default:
break;
}
}
}
[/java]
The whole working application can be downloaded from here the OP in this thread: -
http://hub.jmonkeyengine.org/groups/android/forum/topic/is-android-viable/
I don’t think there’s anything inherently wrong with what you’re doing, but it could be easier too (I think). Can’t you just call onDestroy()?
@sbook
Somehow you have to tell the android activity to finish. I don’t believe you can call onDestroy() directly. The onDestroy is called back to your activity by the android system after something calls the finish() method for the activity.
@jawfin
Thanks for the reply. This is a little different that what I am trying to do. The reason your example works is because there is a touchinput listener already defined for the back button (defined in OGLESContext for android apps) that is registered to AndroidHarness. Because that is there, you can override the onTouch in your android activity to either not display the default dialog box or create a different dialog box to display.
The problem I had was that there wasn’t a listener defined for the menu button on android and referring to InputManager from the Android Activity was returning null. I assume this is because it wasn’t created yet. Last night I solved it by creating my own interface class and implemented it in the android activity. The android activity sets a variable in the game class that is the type of my new interface. The game class then using the variable to fire a callback and passes it the onTouch event from the TouchListener I setup for the menu button. Probably not the most efficient way to do it, but I can then use the menu button for android if the variable is not null or call a nifty screen if it is running on a pc if it is null.
Because nifty is dragging down the performance of the app on android, I’m having to find various ways to call back to the android activity for user interface functions.
Below is the basic idea I did. I love to do this a better way, but not sure how.
New Interface Class:
[java]
public interface AndroidTouchListener {
public void onHUDTouch(String name, TouchEvent evt, float tpf);
}
[/java]
Android Activity:
[java]
public class GameActivity extends AndroidHarness implements AndroidTouchListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
((Main) app).setAndroidTouchListener(this);
}
public void onHUDTouch(String string, TouchEvent te, float f) {
// fire up the android activity to display settings or other uif things
}
}
[/java]
Game Class:
[java]
public class Main extends SimpleApplication implements TouchListener {
AndroidTouchListener androidTouchListener;
public void setAndroidTouchListener(AndroidTouchListener androidTouchListener) {
this.androidTouchListener = androidTouchListener;
}
@Override
public void simpleInitApp() {
// normal init stuff for game
inputManager.addMapping(“TOUCH_MENU”, new TouchTrigger(TouchInput.KEYCODE_MENU));
inputManager.addListener(this, new String[]{“TOUCH_MENU”});
}
public void onTouch(String name, TouchEvent event, float tpf) {
if (androidTouchListener != null) {
androidTouchListener.onHUDTouch(name, event, tpf);
} else {
// do nifty version of settings screen. If null, then is a PC version
}
}
[/java]