{"id":73,"date":"2012-03-20T16:58:26","date_gmt":"2012-03-20T16:58:26","guid":{"rendered":"http:\/\/tier7.net\/blog\/?p=73"},"modified":"2012-03-20T16:58:26","modified_gmt":"2012-03-20T16:58:26","slug":"android-phonegap-cordova-1-5-menubutton-and-searchbutton-with-jquery-mobile","status":"publish","type":"post","link":"https:\/\/tier7.net\/blog\/2012\/03\/android-phonegap-cordova-1-5-menubutton-and-searchbutton-with-jquery-mobile\/","title":{"rendered":"(Android) PhoneGap Cordova 1.5 menubutton and searchbutton with jQuery Mobile"},"content":{"rendered":"<p>Using PhoneGap (Cordova) 1.5, I ran into an issue with the physical buttons on the Android device, specifically I was trying to bind handlers to &#8220;menubutton&#8221;, &#8220;searchbutton&#8221;, and use them to navigate within a jQuery Mobile app. However, these buttons only worked on the first\/initial jQuery Mobile page. After this page was navigated from, the buttons no longer worked.<\/p>\n<p>After attempting many different solutions and workarounds, I came across a very simple solution.<\/p>\n<p>A few lines from the bottom of the <em>cordova.js (1.5.0)<\/em> file, you will find the line:<\/p>\n<pre>\/\/ boot up once native side is ready\r\nchannel.onNativeReady.subscribe(_self.boot);<\/pre>\n<p>Change it to:<\/p>\n<pre>\/\/ boot up once native side is ready\r\nchannel.onNativeReady.subscribeOnce(_self.boot);<\/pre>\n<p>I believe this has something to do with a conflict in the way jQuery Mobile binds to the document.<\/p>\n<p>For good measure, here is the JavaScript I used in the <em><strong>&lt;head&gt;<\/strong><\/em> of my index.html<\/p>\n<pre>$(document).bind(\"mobileinit\", function(){\r\n\u00a0\u00a0 \u00a0\/\/first init\r\n});\r\n\r\n$(document).on(\"pageinit\", function(){\r\n\u00a0\u00a0 \u00a0\/\/each jqm page init\r\n});\r\n\r\n\/\/From PhoneGap's documentation on creating listeners for buttons\r\nfunction init(){\r\n\u00a0\u00a0 \u00a0document.addEventListener(\"deviceready\", onDeviceReady, false);\r\n}\r\n\r\n\/\/when phonegap is ready\r\nfunction onDeviceReady() {\r\n    \/\/event listeners for buttons\r\n\u00a0\u00a0\u00a0 document.addEventListener(\"menubutton\", menuButton, false);\r\n\u00a0\u00a0 \u00a0document.addEventListener(\"searchbutton\", searchButton, false);\r\n}\r\n\r\n\/\/when menu button is pressed\r\nfunction menuButton() {\r\n    \/\/change to main menu page\r\n\u00a0\u00a0\u00a0 $.mobile.changePage(\"#menu\", {transition: \"slidedown\"});\r\n}\r\n\r\n\/\/when search button is pressed\r\nfunction searchButton() {\r\n    \/\/change to search page\r\n\u00a0\u00a0\u00a0 $.mobile.changePage(\"#search\", {transition: \"slideup\"});\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Using PhoneGap (Cordova) 1.5, I ran into an issue with the physical buttons on the Android device, specifically I was trying to bind handlers to &#8220;menubutton&#8221;, &#8220;searchbutton&#8221;, and use them to navigate within a jQuery Mobile app. However, these buttons &hellip; <a href=\"https:\/\/tier7.net\/blog\/2012\/03\/android-phonegap-cordova-1-5-menubutton-and-searchbutton-with-jquery-mobile\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[16,12,19,7,4,20,17,13],"class_list":["post-73","post","type-post","status-publish","format-standard","hentry","category-mobile-development","tag-android","tag-cordova","tag-html5","tag-javascript","tag-jquery","tag-jquery-mobile","tag-mobile","tag-phonegap"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/tier7.net\/blog\/wp-json\/wp\/v2\/posts\/73","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tier7.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tier7.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tier7.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tier7.net\/blog\/wp-json\/wp\/v2\/comments?post=73"}],"version-history":[{"count":1,"href":"https:\/\/tier7.net\/blog\/wp-json\/wp\/v2\/posts\/73\/revisions"}],"predecessor-version":[{"id":74,"href":"https:\/\/tier7.net\/blog\/wp-json\/wp\/v2\/posts\/73\/revisions\/74"}],"wp:attachment":[{"href":"https:\/\/tier7.net\/blog\/wp-json\/wp\/v2\/media?parent=73"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tier7.net\/blog\/wp-json\/wp\/v2\/categories?post=73"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tier7.net\/blog\/wp-json\/wp\/v2\/tags?post=73"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}