Handle Android Back Button on Phonegap InAppBrowser

匿名 (未验证) 提交于 2019-12-03 01:08:02

问题:

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 () { })

回答1:

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



回答2:

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 ); }


回答3:

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.



回答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.



回答5:

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", onB  
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!