I am trying to invoke a Java Method from my Javascript code. This is for a Windows Phone 7 app using Phonegap.
I have the following in my javascript code.
document.addEventListener("backbutton", onBackKeyDown, false);
function onBackKeyDown(){
}
And in my Java code I have the following.
public static native void exportStaticMethod() /*-{
$wnd.onBackKeyDown =
$entry(this.@com.mycompany.myapp.client.MyApp::hideSettingsWidgets());
}-*/;
Then in the on onModuleLoad() I am calling it like so:
MyApp.exportStaticMethod();
It does not work I have an alert in the hideSettingsWidgets()
but it never gets shown.
*EDIT* Here is some more code. The EventListener is not added in the Javascript. It is specifically added withing the java code. I could not get the listeners to register originally so here is what I added.
public static native void removeBackListener() /*-{
$wnd.removeTheListener();
}-*/;
And in my JavaScript
function removeTheListener(){
document.removeEventListener("backbutton", onBackKeyDown, false);
}
Here is my call to hideSettingsWidgets()
public void hideSettingsWidgets(){
for(int i=0;i<settingsScreenWidgets.length;i++){
settingsScreenWidgets[i].setVisible(false);
}
alertString("Working");
removeBackListener();
}
And I am calling the method you gave me inside showSettingsWidgets()
p
rivate void showSettingsWidgets(){
for(int i=0;i<settingsScreenWidgets.length;i++){
settingsScreenWidgets[i].setVisible(true);
}
setCurrentImage();
setOnOffImage();
setupJavaHandler();
}
It does seem to be adding the EventListener that is inside your
public native void setupJavaHandler() /*-{
var app = this;
var onBackKeyDown = $entry(function() {
app.@com.mycompany.myapp.client.MyApp::hideSettingsWidgets();
});
$doc.addEventListener("backbutton", onBackKeyDown, false);
}-*/;
So I am not sure where I am going wrong. I did not add the ArrayList<> you mentioned because was not sure to and the Event Listener was not running when page was Loaded.
Seems like showSettingsWidgets()
never gets run
The
addEventListener
code is probably running when the page loads, right? This will map your empty functiononBackKeyDown
to the backbutton event. Then, when your module loads, you attempt to redefine theonBackKeyDown
function to be a new one - but the old one was already attached to the event you are trying to listen to.This is roughly the equivalent of this (with strings instead of listener functions):
To fix this, you need a cross between what you are doing in your other question, Adding Eventlisteners to document with GWT JSNI, and what you are doing here. Wrapping the Java function in an
$entry
call, and passing that to$doc.addEventListener
makes the most logical sense (though I don't know a lot about WP7).One more thing - remembering that we are writing JavaScript in that native code, what is going to be
this
when thathideSettingsWidgets()
method is called? JavaScript doesn't know that all Java instance methods need athis
to run on (and JavaScript has no problem running methods for object A on B -A.method.call(B)
is totally legal, and often helpful). We need to be sure thatthis
means what we think it does:Edit: Oops, turns out your method was static anyway, so
this
doesn't actually mean anything! Either changeexportStaticMethod
/setupJavaHandler
to be non static and call it directly (probably in youronModuleLoad
as you have it now), or pass in an instance to callhideSettingsWidgets()
on, like we are doing withapp
in the previous sample.or