facebook

Executing javascript code when backbutton is pressed

  1. MobiOne Archive
  2.  > 
  3. Getting Help – General
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #338307 Reply

    SagarSule
    Member

    Hi,

    I am new to MobiOne. I have written an app for Android. However, I want to execute a piece of code(javascript) when my app closes and/or when Home button is pressed. There are functions in appname_custom.js for documentReadyHandler, prePageTransition etc. Is there a function like say onExit or maybe onBackButtonPressed or something?

    I understand that Mobione includes PhoneGap/Cordova api. However the following code does not work (taken from http://docs.phonegap.com/en/1.1.0/phonegap_events_events.md.html)

    document.addEventListener(“backbutton”, onBackKeyDown, false);

    function onBackKeyDown() {
    // Handle the back button
    }

    Kindly help.

    Thanks

    #338317

    Brandon
    Member

    Actually, if you are using a mobione button you are better off using the properties on click and setting that to Run Javascript.
    then you can assign any page using:

    phoneui.gotoPage(‘#m1-pageName’);

    as well as running any javascript or function. So you could use your function inside the onClick – Run Javascript:

    function onBackKeyDown() {
    alert(‘We will now go to a new page’);
    phoneui.gotoPage(‘#m1-pageName’);
    }

    But, if you still need it here is the javascript go back code:
    window.history.back()

    Android does not really have an on exit call it uses.
    But you could also try this as you may be trying to add the listener before the device is ready:

    Add this to script to get device ready call:

    document.addEventListener(“deviceready”, onDeviceReady, false);

    Add event listener to back button and add your code to that call:

    function onDeviceReady(){
    document.addEventListener(“backbutton”, onBackKeyDown, false);
    }
    function onBackKeyDown(){
    alert(‘back’);
    return false;
    }

    #338337

    SagarSule
    Member

    Thanks CincyPlanet! It worked like a charm!!

    However, this does not work in the Test Center! I had to compile an App and run it on my Tablet.

    Is this a bug in the Test Center or its just one of those things not implemented?

    #338339

    Brandon
    Member

    You are welcome.
    I believe it is device specific so its not implemented, most phonegap api calls will not work in the simulator.

Viewing 4 posts - 1 through 4 (of 4 total)
Reply To: Executing javascript code when backbutton is pressed

You must be logged in to post in the forum log in