2

I am new to jQmobile and have been following the examples on the jqMobile site. Using a multi-page template format, I am trying to get the back button to work (hardware button). However, when I am on the 2nd page and I press the (hardware) back button, it simply exits the app, instead of returning to the first page. Here's the sample code I am using:

    <head>
<title>PhoneGap</title>
<script type="text/javascript" charset="utf-8" src="phonegap-1.2.0.js"></script>
<meta name="viewport" content="width=device-width,height=device-height,initial-scale=1"> 

<link rel="stylesheet" href="http://code.jquery.com/mobile/1.0/jquery.mobile-1.0.min.css" />
<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.4.min.js"></script>

<script type="text/javascript">
//This is your app's init method. Here's an example of how to use it
function init() {

    document.addEventListener("deviceready", onDR, false);

} 

function onDR(){
   //document.addEventListener("backbutton", backKeyDown, true);
   navigator.notification.alert("PhoneGap is working");
    //boot your app...

}

function backKeyDown() { 

    // do something here if you wish    
}



$(document).bind("mobileinit", function(){
  $.mobile.touchOverflowEnabled = true;
  $.mobile.defaultPageTransition = 'fade';
});



</script>
<script type="text/javascript" src="http://code.jquery.com/mobile/1.0/jquery.mobile-1.0.min.js"></script>
</head>

<!-- Start of first page -->
<div data-role="page" id="foo">

    <div data-role="header">
        <h1>Foo</h1>
    </div><!-- /header -->

    <div data-role="content">   
        <p>I'm first in the source order so I'm shown as the page.</p>      
        <p>View internal page called <a href="#bar">bar</a></p> 
    </div><!-- /content -->

    <div data-role="footer">
        <h4>Page Footer</h4>
    </div><!-- /footer -->
</div><!-- /page -->


<!-- Start of second page -->
<div data-role="page" id="bar">

    <div data-role="header">
        <h1>Bar</h1>
    </div><!-- /header -->

    <div data-role="content">   
        <p>I'm the second in the source order so I'm hidden when the page loads. I'm just shown if a link that references my ID is beeing clicked.</p>      
        <p><a href="#foo">Back to foo</a></p>   
    </div><!-- /content -->

    <div data-role="footer">
        <h4>Page Footer</h4>
    </div><!-- /footer -->
</div><!-- /page -->
</body>

Thanks, in advance, for all replies.

azsl1326
  • 1,410
  • 2
  • 13
  • 23
  • I don't think it's possible to change the behavior of the hardware button of a smartphone, specially with javascript.. and btw.. where is your javascript? you've only posted markup... – MilkyWayJoe Dec 14 '11 at 20:25
  • Galaxy SII as well as tested on the emulator. I added the javascript to the OP. – azsl1326 Dec 14 '11 at 23:03

1 Answers1

1

It is possible your device does not support the hashchange event.

You can check the compatibility of your device with the hashchange event which is used to update the hash:

if ("onhashchange" in window) {
  //...
}

Source: jQuery - hashchange event

From the jQuery Mobile Docs:

Hash changes that occur independently of a click, such as when a user clicks the back button, are handled through the hashchange event, which is bound to the window object using Ben Alman's hashchange special event plugin (included in jQuery Mobile). When a hash change occurs (and also when the first page loads), the hashchange event handler will send the location.hash to the $.mobile.changePage() function, which in turn either loads or reveals the referenced page.

Source: http://jquerymobile.com/test/docs/pages/page-navmodel.html

Community
  • 1
  • 1
Jasper
  • 75,717
  • 14
  • 151
  • 146
  • I have added the and javascript code. I have tested on both the emulator and my Galaxy SII and the back button doesn't seem to work in the manner I was expecting. – azsl1326 Dec 14 '11 at 22:03