1

Well i'm newbie & stuck at a point. And that might have simple solution as well..

I load a page using window.open using InAppBrowser, While user press hardware backbutton I want to show confirm message before app gets close. I tried some code... Here is Code

index.html

$(document).ready(function() {
    document.addEventListener("deviceready", onDeviceReady, false);

    function onDeviceReady() {

       var ref = window.open('http://stackoverflow.com/', '_blank', 'location=no,hardwareback=yes');
       ref.addEventListener('loadstart', function(event) { SpinnerPlugin.activityStart("Initializing..."); });
       ref.addEventListener('loadstop', function(event) { SpinnerPlugin.activityStop(); });
       ref.addEventListener('loaderror', function(event) { alert('error: ' + event.message); SpinnerPlugin.activityStop();});
       ref.addEventListener('exit', function(event) {
        alert('Exit')
        });
       ref.addEventListener("backbutton", function () { //But This is not triggering...Don't know why..
         onConfirm();
      })
       function onConfirm(button) {
            if (button == 1){ //Yes button pressed...
                navigator.app.exitApp();
            } else{
              return false;
            }
        }
    }
});

Even I tried backbutton event of Cordova..

document.addEventListener("backbutton", onBackKeyDown, false); //Listen to the User clicking on the back button
function onBackKeyDown(e) {
    navigator.notification.confirm("Are you sure you want to exit ?", onConfirm, "Confirmation", "Yes,No");

}

It gets triggered properly but after closing inner window.. So again in brief, I want to show same confirm box before closing inner window.

Note: I'm using Cordova 6.1.0

Thanks a-ton in-Advance

Niks Jain
  • 1,617
  • 5
  • 27
  • 53

1 Answers1

-1

I tried this , it ll work,

document.addEventListener("backbutton", function(e){
        navigator.notification.confirm("Are you sure you want to exit the application?",fnLogout,"Warning","Ok,Cancel");
    }, false);



function fnLogout(button) {
    if(button == 1) {
        navigator.app.exitApp();
    } else {
        return;
    }                     
 }

EDIT: it’s highly possible that overriding the back-button for the InAppBrowser in PhoneGap is not possible.

Try to get brief details in below link: https://cordovablogsblogs.wordpress.com/2016/02/04/handle-android-back-button-on-phonegap-inappbrowser/

Thanks.

User Learning
  • 3,165
  • 5
  • 30
  • 51
  • @Leaner: It gets triggered properly but after inner window gets closed only. Did you try/check my above code using window.open? Thanks Learner.. – Niks Jain Apr 18 '16 at 12:32
  • @Niks so ur trying to close app directly without closing the browser? ie. a single back button to ask dialog for closing application ? – User Learning Apr 19 '16 at 05:34
  • Basically When inAppBrowser get loaded & user try to press backbutton. inner browser gets closed. What i want is, before closing inner browser window it should show confirm yes,no then it should proceed. – Niks Jain Apr 19 '16 at 06:03
  • 1
    @Niks kindly see this link http://stackoverflow.com/a/16569776/4609016 the question +1 u have asked is faced by many people, so currently i think its not possible. – User Learning Apr 19 '16 at 06:50
  • Ohh..So bad..It was raised in 2013 but still no solution or up-gradation for the same..Emm..Any other solution do you see for me?..Plz help..Thanks again. – Niks Jain Apr 19 '16 at 06:58