Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Web and WebWorks Development

Reply
Contributor
PabloBB89
Posts: 13
Registered: ‎11-27-2012
My Device: BB-9300, OS6
My Carrier: Vodafone
Accepted Solution

hardware back button in Webwork app

[ Edited ]

Hello, I have seen some post about this but none of them have solved my issue. I´m not able to trap the back button event on my webworks app. My app just exits on back key pressed . The problem only happens in the real device, with the Chrome plugin its working fine the same code.

 

Edit: I have a BB 9300 os6, and another one with os5, doesnt work on both.

 

My config.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<widget xmlns="http://www.w3.org/ns/widgets"
        xmlns:rim="http://www.blackberry.com/ns/widgets"
        version="2.0"
	rim:header="RIM-Widget: rim/widget">
  <author href="http://www.example.com/"
          rim:copyright="Copyright 1998-2012 My Corp">My Corp</author>
  <name>FIDS_V3</name>
  <icon src="img/icon.png"/>
  <content src="index.html"/>
  <access subdomains="true" uri="*"/>
<rim:loadingScreen    
    backgroundImage="img/imgFIDS.png"
     foregroundImage="img/ajax-loader.gif"
     onRemotePageLoad="true"
     onLocalPageLoad="true"
     onFirstLaunch="true">
</rim:loadingScreen>

<rim:allowInvoke Params="true" />

<rim:connection timeout="60000">
<id>TCP_WIFI</id>
<id>TCP_CELLULAR</id>
<id>BIS-B</id>   
<id>MDS</id>
<id>WAP2</id>
<id>WAP</id>
</rim:connection>  
<rim:cache disableAllCache="true"/>
  <feature id="blackberry.app" required="true" version="1.0.0.0"/>
  <feature id="blackberry.app.event" required="true" version="1.0.0.0"/>
  <feature id="blackberry.system" required="true" version="1.0.0.0"/>
  <feature id="blackberry.system.event" required="true" version="1.0.0"/>
  <feature id="blackberry.identity" required="true" version="1.0.0"/>
  <feature id="blackberry.push" required="true" version="1.0.0"/>
  <feature id="blackberry.utils" required="true" version="1.0.0.0"/>
  <feature id="blackberry.invoke" required="true" version="1.0.0.0"/>
  <feature id="blackberry.io.file" required="true" version="1.0.0.0"/>
  <feature id="blackberry.io.dir" required="true" version="1.0.0.0"/>
</widget>

 

and the javascript code used in index.html

 

   <script>
	function trapForBackKey(){
		blackberry.system.event.onHardwareKey(blackberry.system.event.KEY_BACK,function(){alert('back event trapped3');});
	}
</script>

 

Please use plain text.
BlackBerry Development Advisor
chadtatro
Posts: 619
Registered: ‎10-01-2009
My Device: Z10
My Carrier: Bell

Re: hardware back button in Webwork app

Where/what is calling your trapForBackKey method?

Chad Tetreault | App Development Consultant | BlackBerry Developer Relations | developer.blackberry.com | @chadtatro
Please use plain text.
Contributor
PabloBB89
Posts: 13
Registered: ‎11-27-2012
My Device: BB-9300, OS6
My Carrier: Vodafone

Re: hardware back button in Webwork app

[ Edited ]
<html>
<head>
   <script>
$('#pagMenu').live('pagebeforecreate',function(event){setTimeout(function () {trapForBackKey();}, 200);});
</script> <script> function trapForBackKey(){ blackberry.system.event.onHardwareKey(blackberry.system.event.KEY_BACK,function(){alert('back event trapped3');}); } </script> </head> <body> <div id="pagMenu" data-role="page"> [...] </div> </body> </html> Other thing I tryed, without result. Both ways are working on Crhome webworks simulator. <body> <script> blackberry.system.event.onHardwareKey(blackberry.system.event.KEY_BACK,function(){alert('back event trapped3');}); </script> <div id="pagMenu" data-role="page"> [...] </div> </body>

 

Please use plain text.
Developer
SumiGhosh
Posts: 435
Registered: ‎09-20-2011
My Device: Z10 LE, Playbook, DevAlpha C, 9900, 9380
My Carrier: Vodafone

Re: hardware back button in Webwork app

Try this....

 

<html>
<head>
<script>
$('#pagMenu').live('pagebeforecreate',function(event){setTimeout(function () {trapForBackKey();}, 200);});
</script>
<script>
function trapForBackKey(){
blackberry.system.event.onHardwareKey(blackberry.system.event.KEY_BACK,function(){alert('back event trapped3');});
}

 

 trapForBackKey();
</script>
</head>
<body>
<div id="pagMenu" data-role="page">
[...]
</div>
</body>
</html>

Please use plain text.
Contributor
PabloBB89
Posts: 13
Registered: ‎11-27-2012
My Device: BB-9300, OS6
My Carrier: Vodafone

Re: hardware back button in Webwork app

[ Edited ]

Thank you for answering.

 

The problem was that I wasnt including this page (index.html) in the webworks package. The web is in a webserver and I put in the config.xml the content pointing to that webserver. Is this a bad practice? The problem of adding the web project to the package is that to make changes you have to make a whole update of the aplication instead of changing just the server.

 

Apart from this, now my problem is that with

 

blackberry.system.event.onHardwareKey(blackberry.system.event.KEY_BACK,function handleBack(){history.back();return false;});

 When I press back in the first page the app doesnt close as I expected it to do. Is there a way to ask like

 

if (history.canGoBack()){
   history.back()
}else{
   exit();
}

 How is this controled in bb?

Please use plain text.
Contributor
PabloBB89
Posts: 13
Registered: ‎11-27-2012
My Device: BB-9300, OS6
My Carrier: Vodafone

Re: hardware back button in Webwork app

Does any body know?

Please use plain text.
BlackBerry Development Advisor
chadtatro
Posts: 619
Registered: ‎10-01-2009
My Device: Z10
My Carrier: Bell

Re: hardware back button in Webwork app

If you're capturing the back button, and the user presses it from the first page (where they would expect the app to exit), you could do something like this:

 

Check the location.href.  If it's index.html than exit, if not do something else.  You'll need to manually check this if you're overriding the back button event.

 

if (location.href === 'index.html') {
  blackberry.app.exit();
} else {
    // do something else
}

 

Chad Tetreault | App Development Consultant | BlackBerry Developer Relations | developer.blackberry.com | @chadtatro
Please use plain text.