19

I am using PhoneGap 1.5.0, jQuery 1.7.1 and jQuery mobile 1.0.1 and trying to override the backbutton in Android as stated here or here.

document.addEventListener("deviceready", onDeviceReady, false);
// PhoneGap loaded

function onDeviceReady() {
console.log("PhoneGap Ready!");
// waiting for button
document.addEventListener("backbutton", handleBackButton, false);
}

// handle the back button
function handleBackButton() {
console.log("Back Button Pressed!");
navigator.app.exitApp();
}

But it only works on the first page of my app. After changing to a different page the backbutton does nothing at all. The app consists of a tabview like this:

<body>
<div data-role="page" id="pilotTab">
    <div data-role="header">
        <h1>Pilot</h1>
    </div>
    <div data-role="content" id="pilotContent">
content be here ;)
    </div>
    <div data-role="footer" data-position="fixed">
        <div data-role="navbar">
            <ul>
                <li><a href="pilotTab.html" data-transition="none">Lotse</a>
                </li>
                <li><a href="bookingTab.html" data-transition="none">Verkehr</a>
                </li>
                <li><a href="mainListTab.html" data-transition="none">B&ouml;rt</a>
                </li>
            </ul>
        </div>
        <!-- /navbar -->
    </div>
    <!-- /footer -->
</div>

Is it a stupid mistake or is there something special I have to consider to make it work properly? Thanks in advance.

Community
  • 1
  • 1
Redfox
  • 1,024
  • 1
  • 11
  • 28
  • the code works as it is for me. do you see the log message when you click the back button on other pages?? – dhaval Mar 09 '12 at 10:00
  • sadly I don't get any output in the logfile on other pages than the first :( Thx for helping :) – Redfox Mar 09 '12 at 10:31
  • 1
    i have put my test code in a gist https://gist.github.com/2006096 you can see and check if anything is missing – dhaval Mar 09 '12 at 11:06
  • did you put the required javascript files in `/js` directory. without that it is not going to work – dhaval Mar 09 '12 at 12:17
  • Thx for you code. It works just fine in a little demo project. Unfortunately the error of mine must be somewhere else then. :( – Redfox Mar 09 '12 at 12:34
  • Which version of PhoneGap were you using for testing? The code is working in an older testproject of mine running phoneGap 1.4.0. But I cannot get it to work in my current project :( – Redfox Mar 09 '12 at 14:58
  • 1
    i m using 1.4. I checked the new version (1.5) and it seems the backbutton functionality is broken in that. – dhaval Mar 09 '12 at 18:09
  • after moving from 1.2.0 to 1.5.0 the back button has broken for me. – Skylar Saveland Mar 12 '12 at 03:57
  • @skyl i have post the changes i have done to 1.5.0 to make it work. can you try and see if they are working for you. – dhaval Mar 12 '12 at 06:52

3 Answers3

12

I gone through the new Phonegap source code and did following changes to make the backbutton work.

Html test code

<script type="text/javascript">
    $("#home").click(function(){
        $.mobile.changePage("home.html");
    });

    document.addEventListener("deviceready", onDeviceReady, false);
    document.addEventListener("backbutton", handleBackButton, false);

    function onDeviceReady() {
        console.log("PhoneGap Ready!");
    }

    function handleBackButton() {
        console.log("Back Button Pressed!");
        navigator.app.exitApp();
    }
</script>

Put the following code in the else block of document.addEventListener in cordova-1.5.0.js after line-no 507

if (e === 'backbutton') {
    var exec = require('cordova/exec')
    exec(null, null, "App", "overrideBackbutton", [true]);
}

Put following code in fireDocumentEvent method of cordova definition in cordova-1.5.0.js after line-no 592

if(type == "backbutton"){
    var e = document.createEvent('Events');
    e.initEvent(type);
    if (data) {
        for (var i in data) {
            e[i] = data[i];
        }
    }
    document.dispatchEvent(e);
    return;
}

I have put the whole cordova-1.5.0.js in this gist with updated code https://gist.github.com/2020325

Though it is working for me but it still may need some changes to work in all the possible scenarios.

Edit

Put following code in fireDocumentEvent method of cordova definition in cordova-1.5.0.js after line-no 592

if(type == "backbutton" || type == "menubutton" || type == "searchbutton"){
        var e = document.createEvent('Events');
        e.initEvent(type);
        if (data) {
            for (var i in data) {
                e[i] = data[i];
            }
        }
        document.dispatchEvent(e);
        return;
    }
dhaval
  • 7,611
  • 3
  • 29
  • 38
  • works like a charm. a bazillion thanks! :) Starting the app under IOS with these changes, a popup "idllbackbutton, app0, true" starts up. But as you said, a few modification should get rid of that. Do you know by chance, how I could create something like "do only on ios" in the phonegap source? – Redfox Mar 12 '12 at 08:21
  • i will check in ios but generally the popup comes when the JS and native library are of different version. Are you sure the iOS project is using Phonegap 1.5 native library – dhaval Mar 12 '12 at 08:25
  • 1
    Actually `backbutton` event should not fire for iOS – dhaval Mar 12 '12 at 08:31
  • 1
    there is a code difference between cordova android and iOS js files, so you may not want to copy the same js file for both projects. – dhaval Mar 12 '12 at 08:49
  • 1
    Ah, I see, that is not quite what I expect from a "write once, run everywhere" thingy like phoneGap. But well... Thx again. – Redfox Mar 12 '12 at 09:22
  • This does not fix the bug for me. I c/p'ed the code from gist, so no typos possible... I created an [Apache issue report](https://issues.apache.org/jira/browse/CB-318) – Florian Mar 13 '12 at 18:02
  • @Florian actually it is working for me, can you put your source somewhere so that i can test with that. also let me know where are you testing device/simulator – dhaval Mar 14 '12 at 20:01
  • Does your hack work also for menubutton and searchbutton?! I can't get them work! – Fabio Buda Mar 19 '12 at 14:28
  • @FabioBuda i have put the updated code block in edit section, it works for searchbuttona and menubutton as well – dhaval Mar 19 '12 at 18:03
  • This solution fixes Phonegap/Cordovo 2.0.0 on Android JellyBean as well. – misterbee Jul 21 '12 at 23:47
  • Is in 2.0.0, issue is fixed?? Because i am trying and not able to handle the backbutton at all – Arindam Mukherjee Oct 02 '12 at 10:16
  • I have the issue with Cordova 2.1.0 on Jelly Bean as well. – mld Nov 01 '12 at 00:39
8

Bryce Curtis is suggesting on that page to just change the line :

channel.onNativeReady.subscribe(_self.boot);

to :

channel.onNativeReady.subscribeOnce(_self.boot);

at the end of the file.

That seems to do the trick for me and fixed the backbutton AND the menubutton and searchbutton !

YuDroid
  • 1,599
  • 5
  • 22
  • 44
Guillaume Gendre
  • 2,504
  • 28
  • 17
  • Hi, This resolved menubutton click issue for me. But I get the following error message on the console. "Uncaught TypeError: Object [object Object] has no method 'subscribeOnce' at file:///android_asset/www/thirdparty/cordova-2.4.0.js:6524" Please throw some light on it! Also can this error be ignored without any sideeffects? – Priya Kathir Feb 25 '13 at 06:42
0

@dhaval : I have done the following changes in the cordova-1.5.0.js in Android.

The pages where I have not handled back button is working fine, But the place where I am handling the Back button does not work.

Its not even able to pick the function

function handleBackButton() {

  console.log("Back Button Pressed!");
    navigator.app.exitApp();
}
Surya
  • 439
  • 3
  • 9
  • 31
  • 1
    using cordova 2.0, the back button handling works as described without any changes necessary in the framework (at least for me). I did it like this: document.addEventListener("deviceready", onDeviceReady, false); function onDeviceReady() { document.addEventListener("backbutton", handleBackButton, false); } function handleBackButton() { yourBackButtonHandlingFunction(); } (BTW: any way to get a decent formatting in the comments? :/) – Redfox Sep 04 '12 at 11:50
  • @Redfox You can use backticks (\`) for code formatting, `like this`. – MBillau Apr 16 '13 at 14:37