0

I am developing an app where i need to call some methods from blackberry native to javascript.

when i click on back key down event , i want to trigger the onBackKeyDown() method, which is declared in javascript.

Main.java

protected boolean keyDown(int keycode, int time) {
    // TODO Auto-generated method stub

    if(Keypad.key(keycode) == Keypad.KEY_ESCAPE)
    {
           // onBackKeyDown(); 
         // i want to call the following method which is declared in main.js file 
        Dialog.alert("this is back button");        
        return true;

    }
    return super.keyDown(keycode, time);
}

main.js

 function onBackKeyDown() {
   try {
       if ($.mobile.activePage.is("#Page1")) {
         $.mobile.changePage("#page5");
        } else if ($.mobile.activePage.is("#page2")) {
           $.mobile.changePage("#main");
       } else if ($.mobile.activePage.is("#page3")) {
           $.mobile.changePage("#main");
       } else if ($.mobile.activePage.is("#main")) {
           navigator.app.exitApp();
       }
} catch(e) {
    alert("Exception:ConsoleLog.log:" + e);
}

}

As i am having idea that by using "extendScriptEngine" , the methods declared in javascript are invoked in native. But here how to invoke the methods in javascript which are in native as per my above code... can anyone please help me with this...

code_finder
  • 1,340
  • 1
  • 21
  • 39

1 Answers1

0

You don't show this code, but I have to assume that your app has some Screen that contains some kind of browser field, which is displaying HTML content.

I can't tell you for sure without seeing that code, but what I would recommend is to use net.rim.device.api.browser.field2.BrowserField (Browser Field 2), if your app only needs to support OS 5.0 and higher.

If you have to support less than OS 5.0, I'm not sure how to do that.

Anyway, with this 5.0+ BrowserField, you can do this:

BrowserFieldConfig config = new BrowserFieldConfig();
config.setProperty(BrowserFieldConfig.JAVASCRIPT_ENABLED, Boolean.TRUE);  // should be the default

// Browser basic initialization
BrowserField _browserField = new BrowserField(config);

and then

protected boolean keyDown(int keycode, int time)
{
    if(Keypad.key(keycode) == Keypad.KEY_ESCAPE)
    {
        // i want to call the following method which is declared in main.js file 
        _browserField.executeScript("onBackKeyDown()");
        Dialog.alert("this is back button");        
        return true;

    }
    return super.keyDown(keycode, time);
}
Nate
  • 30,589
  • 12
  • 76
  • 201