Default call Phonegap Back Button Handler

I have a phonegap application that needs to grab the back button. This works smoothly, but when I am on the main screen and press the "Back" button, I want to call the source event handler and close the application or do everything that naturally comes to the platform with such a print. I know that I can say that the application is leaving, but I understand that this is a bad form for iPhone applications.

No matter what I try (and I tried a lot of things), I can not start the original handler. Any advice?

In my code, I have a switch statement inside the backbutton event handler that directs the application to the action as necessary:

switch blahBlah { case 'this' : doThis() ; break; case 'main' : // What do I do here that is well behaved for all platforms??? break; default: doFoo() ; } 
+6
source share
4 answers

Detect every time you land on the main screen and delete your own event handler.

 document.removeEventListener( "backbutton", function(){}, false ); 

and add an event listener to other pages (or sections).

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

Hope this helps.

+4
source

This is what I used and seems to work great for my needs

  function pageinit() { document.addEventListener("deviceready", deviceInfo, true); } function deviceInfo() { document.addEventListener("backbutton", onBackButton, true); } function onBackButton(e) { try{ var activePage = $.mobile.activePage.attr('id'); if(activePage == 'Options'){ closeOptions(); } else if(activePage == 'Popup'){ closePopup(); } else if(activePage == 'HomePage'){ function checkButtonSelection(iValue){ if (iValue == 2){ navigator.app.exitApp(); } } e.preventDefault(); navigator.notification.confirm( "Are you sure you want to EXIT the program?", checkButtonSelection, 'EXIT APP:', 'Cancel,OK'); } else { navigator.app.backHistory(); } } catch(e){ console.log('Exception: '+e,3); } } 

Hope this helps ...

+3
source

Cordoba 7.x, at least on Android, does not seem to properly update the override state. As a result, @SHANK's answer no longer works.

As a workaround, you can disable the back button manually overriding, which will lead to the default behavior:

 navigator.app.overrideBackbutton(false); 

For repeated active user processing, similarly do:

 navigator.app.overrideBackbutton(true); 

I logged an error report in Apache error tracking regarding this.

+1
source

An easy and smooth way to do this could be something like this, this statement worked in my case:

 if($("#mainPage").is(".ui-page-active")){ navigator.app.exitApp(); } 

I am using jQuery: http://demos.jquerymobile.com/1.0a3/docs/api/globalconfig.html

welcome :)

-1
source

Source: https://habr.com/ru/post/926022/


All Articles