2013-12-10 48 views
4

我为我的应用程序使用Phonegap,我需要在InAppBrowser 中显示外部链接,但它看起来像后面的按钮并不像预期的那样工作:如果我在做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启用'导航历史记录'吗?

谢谢。

+0

如果小鸡在P精炼返回按钮,它将您带到以前的意图而不是您的Web视图上的上一页。这意味着你必须在你的页面中添加一个后退按钮。 –

回答

0

你可以听后退按钮(假设你在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); 
+1

对我不起作用.. –

2

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

这里是InAppBrowser.java要做出的改变,在showWebPage方法中的“运行”的方法,将有一个监听器的代码是这样的:

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

真的很有帮助,像Cordova 3.0.0上的魅力一样工作 –

+0

我在** InAppBrowser.java **类中找不到'dialog.setOnDismissListener(...)'。 –

0

除了@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得到这个工作