- This topic has 3 replies, 2 voices, and was last updated 12 years, 3 months ago by
Brandon.
-
AuthorPosts
-
SagarSuleMemberHi,
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
April 22, 2013 at 8:13 am #338317
BrandonMemberActually, 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;
}April 22, 2013 at 1:17 pm #338337
SagarSuleMemberThanks 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?
April 22, 2013 at 2:31 pm #338339
BrandonMemberYou are welcome.
I believe it is device specific so its not implemented, most phonegap api calls will not work in the simulator. -
AuthorPosts