8

I would like to disable or override the Android Back button while I am navigating pages on the InAppBrowser. Can I add an event listener that can handle that?

EDIT: Looking at the answer by @T_D below the solutions provided are the closest I could get to. It does not seem to be possible to override the button in InAppBrowser as all the PhoneGap tweaks stop working while navigating pages on this plugin. I was not able to find any other solution rather than modifying the API library. If there are any PhoneGap guys here and know something more, I 'll be glad to get some comment. Thanks.

The closest I got:

var ref = window.open('http://apache.org', '_blank', 'location=yes');
ref.addEventListener("backbutton", function () { })
  • hey, I posted an new answer that works for me, you can check it out below and hopefully will work for you too – Albert May 20 '14 at 17:03

9 Answers9

9

According to the documentation the behaviour of the hardware back button can be configured now for the InAppBrowser:

hardwareback: set to yes to use the hardware back button to navigate backwards through the InAppBrowser's history. If there is no previous page, the InAppBrowser will close. The default value is yes, so you must set it to no if you want the back button to simply close the InAppBrowser.

Thanks to Kris Erickson.

So just update your InAppBrowser plugin if the backward navigation is the desired behaviour.

For more details see: https://github.com/apache/cordova-plugin-inappbrowser/pull/86

Community
  • 1
  • 1
lhaferkamp
  • 676
  • 8
  • 10
  • I am setting the hardwareback = 'no' to simple close the InAppBrowser. It is closing the InAppBrowser but also closing my application. How to go back to my app if my app opens the InAppBrowser? – Setu Kumar Basak Mar 04 '18 at 05:13
7

You can do it quite easily now (as of InAppBrowser version 0.3.3), but you will have to edit the Java files. Go to src/com/org/apache/corodova/inappbrowser directory and edit the InAppBrowserDialog.java:

Change

 public void onBackPressed () {
    if (this.inAppBrowser == null) {
        this.dismiss();
    } else {
        // better to go through the in inAppBrowser
        // because it does a clean up            
        this.inAppBrowser.closeDialog();           
    }
}

to

public void onBackPressed () {
    if (this.inAppBrowser == null) {
        this.dismiss();
    } else {
        if (this.inAppBrowser.canGoBack()) {
            this.inAppBrowser.goBack();
        }  else {
            this.inAppBrowser.closeDialog();
        }
    }
}

Then go to InAppBrowser and find the goBack function, change:

/**
 * Checks to see if it is possible to go back one page in history, then does so.
 */
private void goBack() {
    if (this.inAppWebView.canGoBack()) {
        this.inAppWebView.goBack();
    }
}

to

/**
 * Checks to see if it is possible to go back one page in history, then does so.
 */
public void goBack() {
    if (this.inAppWebView.canGoBack()) {
        this.inAppWebView.goBack();
    }
}

public boolean canGoBack() {
    return this.inAppWebView.canGoBack();
}

And now the hardware back button will go back until there are no more backs to do. I really think this should be the default behavior in android since the Done button already closes the InAppBrowser window.

Kris Erickson
  • 33,454
  • 26
  • 120
  • 175
  • That's a good answer, though there should have been a Javascript workaround, otherwise you lose the point of using PhoneGap. – Dimosthenis Kontogiorgos Mar 09 '14 at 18:14
  • Agreed, and there should probably be an option that you can pass to the open command (as the options already differ widely from ios to android) to allow for both options. – Kris Erickson Mar 09 '14 at 18:18
  • 2
    I've made a pull request for this, and it is available in my branch of InAppBrowser (https://github.com/kriserickson/cordova-plugin-inappbrowser) – Kris Erickson Mar 09 '14 at 18:39
  • @KrisErickson sorry for my ignorance, I'm pretty new with cordova, how do I go about installing your branch of the InAppBrowser plugin in my cordova applicaton? I usually install them via the CLI (`cordova plugin add ...`). What files should I download and where should I put them? Thanks! – Albert May 20 '14 at 16:41
  • @Albert you just use the standard cordova plugin CLI. It should be as simple as `cordova plugin add https://github.com/kriserickson/cordova-plugin-inappbrowser.git`. – Kris Erickson May 20 '14 at 17:07
  • @KrisErickson ok great, thanks! Btw I posted an answer for this question (see below) that doesn't require to edit any Java files – Albert May 20 '14 at 17:46
  • @KrisErickson Thanks! I made it work. But then i made another mistake. So I was forced to start new PG project and couldn't find out how I did it. But you helped me again. Thanks again! – peterdoesco.de May 21 '14 at 08:12
  • It seems that if you listen to the `exit` event in javascript and manually close the browser from the parent, it will accomplish the same thing. – jonperl Jun 11 '14 at 20:36
  • Great answer, totally works. It's better though to go a bit farther and make closing the inAppBrowser with the back button an option though. – hobberwickey Oct 06 '14 at 19:14
  • @KrisErickson I have a problem. I have edited my CordovaWebView to make my application close on back button.How can i use your answer so that it will allow the application on back button ? please refer my question http://stackoverflow.com/questions/28271685/cordova-opening-a-web-page-in-device-browser – CraZyDroiD Feb 02 '15 at 07:45
  • I am setting the hardwareback = 'no' to simple close the InAppBrowser. It is closing the InAppBrowser but also closing my application. How to go back to my app if my app opens the InAppBrowser? – Setu Kumar Basak Mar 04 '18 at 05:14
  • @KrisErickson isn't it kinda setting it in infinite loop? good for performance? just a doubt – minigeek Aug 19 '20 at 11:22
4

EDIT NOTE: As far as I know, it's not possible to override the back-button for the InAppBrowser in PhoneGap. But I did my best searching for possible solutions...

There's an eventListener to override back-button in PhoneGap -doesn't work for InAppBrowser-

function onDeviceReady(){
    document.addEventListener("backbutton", onBackKeyDown, false);
}

Alternative eventListener to override back-button -the OP said this didn't work either-

var ref = window.open('http://www.stackoverflow.com', '_blank', 'location=yes');
ref.addEventListener("backbutton", function () {
    //logic here
})

Overriding the Back-button in an Activity -this is plain java, obviously didn't work in PhoneGap-

@Override
public void onBackPressed()
{
   //logic here
}

Conclusion:

Above solutions didn't work, following links (this answer, this one and a third one) didn't help either. So it's highly possible that overriding the back-button for the InAppBrowser in PhoneGap is not possible. If someone does come up with a solution or if things changed for a new PhoneGap version feel free to let us know...

EDIT:

Installing this plugin may take you to closest solution:

cordova plugin add org.apache.cordova.inappbrowse

What this plugin will do, in WP8, it will overlay back/forward/close button on InAppBrowser whenever you open any link/page in it.

See this image: enter image description here

Community
  • 1
  • 1
T_D
  • 3,241
  • 3
  • 17
  • 24
  • Hey, thanks for the answer. I tried this but it did not really work on Phonegap. There is also a Javascript example for Phonegap (also here in Stack Overflow) but it does not work as long as I open an InAppBrowser instance. – Dimosthenis Kontogiorgos May 20 '13 at 17:36
  • 1
    Oh right, it shouldn't work because Javascript is the way to go for PhoneGap applications. Did you try something like this? `function onDeviceReady(){ document.addEventListener("backbutton", onBackKeyDown, false); }` Or maybe [this answer](http://stackoverflow.com/a/12632712/2140636) can help. Actually I don't know much about PhoneGap yet but I'll do my best to help you out... – T_D May 21 '13 at 08:23
  • Yes I tried that as well. Well, it does work on all the PhoneGap pages but not withing the InAppBrowser. Seems like when the InAppBrowser instances take action, the rest of the PhoneGap rules do not really apply; then again it is still new on PhoneGap. And it seems like nobody else around had a similar problem yet. – Dimosthenis Kontogiorgos May 22 '13 at 18:58
  • First of all. Could you share the code-snippet where you open the InAppBrowser? My next guess would be adding following EventListener: `var ref = window.open('http://apache.org', '_blank', 'location=yes'); ref.addEventListener("backbutton", function () { })` – T_D May 23 '13 at 07:07
  • Nope. It does not work. According to [here](http://docs.phonegap.com/en/2.7.0/cordova_inappbrowser_inappbrowser.md.html#InAppBrowser) there is not such an event in InAppBrowser. – Dimosthenis Kontogiorgos May 29 '13 at 08:36
  • 1
    @LeonardZelig It might help if you'd edit the question to share your code :) Well, I think I found some interesting stuff [here](http://stackoverflow.com/a/9662760/2140636) and [there](http://developer.appcelerator.com/question/56391/how-to-add-event-listener-to-back-button-in-the-navigation-group-) but it's never about handling back-button in the Inappbrowser. Maybe it's simply not possible... – T_D May 29 '13 at 09:23
  • 1
    Hi @T_D, thanks for effort. The closest I could get was the event listener you posted but as said, it did not work either. However it would be nice to see if there is going to be anything new on the next PhoneGap versions. – Dimosthenis Kontogiorgos May 30 '13 at 19:25
  • You're welcome. Well, now it's your choice whether you accept this answer or wait for someone else posting a solid solution. If you want I could start a bounty but I doubt that would make any difference :/ – T_D May 31 '13 at 07:53
  • 1
    I believe the solution is that it is not yet possible, so answer accepted :) – Dimosthenis Kontogiorgos May 31 '13 at 14:43
  • Since the answer is accepted! So, at first I was happy that I found solution. But at the end, :-( – Faizan Mubasher Feb 17 '15 at 13:31
4

This worked for me in PhoneGap 2.7, help came from here, How do I disable Android Back button on one page and change to exit button on every other page

document.addEventListener("deviceready", onDeviceReady, false);
    function onDeviceReady() {
        document.addEventListener("backbutton", function (e) {
            e.preventDefault();
        }, false );
}
Community
  • 1
  • 1
JamesRLamar
  • 908
  • 1
  • 11
  • 22
4

I was having this same issue and finally got it to work, I'll post the answer here in case it helps someone.

This is the code I use:

window.new_window = window.open(url, '_blank', 'location=no');

window.new_window.addEventListener("exit", function () {
    window.new_window.close();
});

So the key basically is to attach the exit event, which gets called when the back button of the device is tapped.

BTW, I used cordova.js, and build my apps locally using the Cordova CLI, I don't know if that makes any difference, I mention it just in case.

Albert
  • 1,516
  • 3
  • 24
  • 55
3

Use jQuery mobile:

$(document).on('backbutton',
 function(e){
     e.preventDefault();
     // YOUR CODE GOES HERE
});
Marcin S.
  • 11,161
  • 6
  • 50
  • 63
3

Running Cordova 5.1.1 and when i load pages in the inappbroswer i like having the back button work until the inappbrowser exits back to my index.html page because it's blank and just sits there. So i used the following code to fix this. It exits the app when it exits the inappbrowser.

window.open = cordova.InAppBrowser.open;                
            var ref = window.open(url, '_blank', 'location=no');
            ref.addEventListener('exit', function () {
                navigator.app.exitApp();
            });
Post Impatica
  • 14,999
  • 9
  • 67
  • 78
0

As far as I know it's not possible to override or detect the back button from inAppBrowser. When you press the back button, inAppBrowser will hide and return control to the Phonegap page. You can catch this with the focus event on the window, (using jQuery) like

var browser = window.open('http://example.com', '_blank', 'location=no');
$(window).on('focus', function() {
    browser.show();
});

to reopen the browser. You could then use browser.executeScript() to signal the webapp loaded in the browser, if you like.

Inspired by this forum post.

wvengen
  • 383
  • 4
  • 10
0

I know this question has an answer already but I post my answer for those who any of these answers didn't work for them(such as myself):

so I have a multi page app for android and IOS and I am using cordova 5.x and I added the code below in every page except the page I needed InAppBrowser:

delete window.open;

and then for the rest of the pages I used:

document.addEventListener("backbutton", onBackKeyDown, false);
function onBackKeyDown(event) {
// your code for handling back button comes here
}

for handling back button

note that: delete window.open; base on the documentation

manually restore the default behaviour of 'window.open'

after that InAppBrowser plugin worked great and I handled back button in all pages correctly.

one last thing don't forget to add:<script type="text/javascript" charset="utf-8" src="cordova.js"></script> in pages you need to have InAppBrowser.

hope this help.

Pouya Danesh
  • 1,557
  • 2
  • 19
  • 36