Reputation: 1006
I am playing with my first PhoneGap/Android app, and am having trouble triggering $.mobile.changePage()
from a PhoneGap/Cordova Event.
I've updated the code, whereas before it would trigger once. Now it does not trigger at all, but is closer to how it probably should be done.
Here is my code:
$(document).on('pageinit', function() {
document.addEventListener("searchbutton", onSearchKeyDown, false); //never fires
document.addEventListener("menubutton", onMenuKeyDown, false); //never fires
alert("triggered"); //does fire
});
// search button press
function onSearchKeyDown() {
//change to search page
alert("search");
$.mobile.changePage("#page4", {transition: "pop"});
}
//menu button press
function onMenuKeyDown() {
//change to start page
alert("menu");
$.mobile.changePage("#page1", {transition: "pop"});
}
Upvotes: 1
Views: 1939
Reputation: 1006
Here is the solution if anyone else has this problem. via https://stackoverflow.com/a/9739986/799876
A few lines up from the bottom of cordova.js, there is the line
channel.onNativeReady.subscribe(_self.boot);
Change it to
channel.onNativeReady.subscribeOnce(_self.boot);
For me, this fixed the menubutton and searchbutton events with phonegap & jQuery Mobile.
Upvotes: 1
Reputation: 28187
Use pageInit()
, not $(document).ready()
.
See the documentation here:
The first thing you learn in jQuery is to call code inside the $(document).ready() function so everything will execute as soon as the DOM is loaded. However, in jQuery Mobile, Ajax is used to load the contents of each page into the DOM as you navigate, and the DOM ready handler only executes for the first page. To execute code whenever a new page is loaded and created, you can bind to the pageinit event. This event is explained in detail at the bottom of this page.
Upvotes: 2