【问题标题】:Phonegap InAppBrowser - back button don't go to previous pagePhonegap InAppBrowser - 后退按钮不转到上一页
【发布时间】:2013-12-10 14:39:51
【问题描述】:

我正在为我的应用程序使用 Phonegap,我需要在 InAppBrowser 中显示一个外部链接 但看起来后按钮没有按预期工作:如果我在做

var ref = window.open('www.example.com/a.html' , '_blank', 'location=no')

a.html 页面我点击了一个指向 www.example.com/b.html 的链接,下次我点击返回时,InAppBrowser 已关闭,但它应该回到a.html

您知道如何为 InAppBrowser 启用“导航历史记录”吗?

谢谢。

【问题讨论】:

  • 如果您点击电话后退按钮,它会将您带到之前的意图,而不是您的 Web 视图上的上一页。这意味着您必须在页面中添加一个后退按钮。

标签: android cordova inappbrowser


【解决方案1】:

这可以通过调整“InAppBrowser.java”来实现。我知道这有点奇怪,但这是我唯一的选择。但是,我对 java 文件所做的那些小调整现在允许我在我的应用程序的页面中导航。

这是在 InAppBrowser.java 中进行的更改,在 showWebPage 方法中的 'run' 方法中,将有一个监听器代码如下:

dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                    public void onDismiss(DialogInterface dialog) {     
                        closeDialog();
                    }
});

在该行下方添加以下代码,

dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {                   
@Override
     public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
         if (event.getAction()!=KeyEvent.ACTION_DOWN)
             return true;               
         if(keyCode==KeyEvent.KEYCODE_BACK){
             goBack();
             return true;
         }else {
             return false;
          }
      }
});

【讨论】:

  • 真的很有帮助,在 Cordova 3.0.0 上的工作就像一个魅力
  • 我在 InAppBrowser.java 类中找不到dialog.setOnDismissListener(...)
【解决方案2】:

您可以监听后退按钮(假设您使用的是 android)并将 history.back 调用注入 InAppBrowser。

document.addEventListener("backbutton", function(){
    //do some checks to make sure the browser is open or 
    //whatever else you may need first, then:
    cordova.exec(null, null, "InAppBrowser", "injectScriptCode", ["history.back()"]);
}, false);

【讨论】:

    【解决方案3】:

    除了@Suresh Raja 写的内容之外,引用的代码不再存在。您可以在这段代码之后添加建议的改进代码(如下):

    dialog.setInAppBroswer(getInAppBrowser());
    

    建议的改进代码:

    dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {                   
    @Override
        public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
              if (event.getAction()!=KeyEvent.ACTION_DOWN)
                  return true;               
              if (keyCode==KeyEvent.KEYCODE_BACK){
                  if (inAppWebView.canGoBack()) {
                      inAppWebView.goBack();
                  }
                  else {
                      closeDialog();
                  }
                  return true;
              } else {
                    return false;
              }
         }
    });
    

    这将在最后一次后按时关闭应用程序(这可以解决 inAppBrowser 的另一个问题。 希望有帮助

    编辑:您应该添加 import android.content.DialogInterface 来完成这项工作。

    【讨论】:

      猜你喜欢
      • 2013-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多