Software Archive
Read-only legacy content
17061 Discussions

Back button closes phone application despite the capture event - Ionic

Mario_R_
Beginner
427 Views

 

Hi all,

when I hit the back button on the phone the application is closed and, when reactivated, it starts from the beginning.

I tried to capture the event in these two ways, that work well within the emulator. Do not go instead in the final build on phone.

//First try
document.addEventListener("backbutton", backButtonPressed, false);
    
function backButtonPressed() {

}


//Second try
document.addEventListener("intel.xdk.device.ready",function() {
       intel.xdk.device.addVirtualPage(); 
    },false);

document.addEventListener("intel.xdk.device.hardware.back", function() {
        intel.xdk.device.addVirtualPage();
    }, false);

 

It needs some plugins for the event, to make it work with Ionic?

thank you

 

0 Kudos
5 Replies
PaulF_IntelCorp
Employee
427 Views

For the Cordova backbutton event capture you do not need to provide any special plugins. To use that older intel.xdk event capture, you'll have to include the intel.xdk.device plugin.

I recommend you use the Cordova API. See this Stack Overflow post for some examples: http://stackoverflow.com/questions/22609411/over-ride-the-back-button-of-android-device-through-phonegap-in-javascript

0 Kudos
Anya_A_
Novice
427 Views

Hi, I'm using Ionic too and have the same problem: the app closes anyway. I'm using event.preventDefault() and

document.addEventListener("backbutton", onBackKeyDown, false);

Even though the code works and onBackKeyDown is called.

Is there any other way to prevent its default behaviour?

Ok, found this link in another topic http://www.gajotres.net/ionic-framework-handling-android-back-button-like-a-pro/ Gonna leave it here in case someone finds this topic first like I did :)

0 Kudos
Anya_A_
Novice
427 Views

Posting this as an update to trying out what was posted in the link. Here is an error I get if I use

 $ionicPlatform.registerBackButtonAction(function(event) {

$ionicPlatform is not defined

and the other one is if I change it with ionic.Platform.

ionic.Platform.registerBackButtonAction is not a function

So I'm back to being stuck again.

 

0 Kudos
Hamilton_Tenório_da_
Valued Contributor I
427 Views

@Mario>

For me, it works good. I have: 

document.addEventListener("backbutton", funNaoFazNada, false);

 

function funNaoFazNada() {
    
}

 

Just it.

0 Kudos
Anthony_H_Intel
Employee
427 Views

Anya-

If I understand correctly

  • registering a backbutton event handler worked
  • the backbutton event handler is called
  • the app exits after the backbutton event handler is called 

Are you using Crosswalk with your app?
Are you testing this using App Preview or have you built your app and installed it on a device?

Update:
Sorry I did not read more carefully; I did not realize you were using Ionic.
I see that you must have found this topic, where Paul pointed to this blog post.
If $ionicPlatform is undefined, there must be something wrong with how Ionic is configured or your code is being called before Ionic is initialized.
 

 

 

0 Kudos
Reply