我正在为我的应用程序使用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 :(得分:2)
通过调整'InAppBrowser.java'可以实现这一点。我知道这有点奇怪,但这是我唯一的选择。但是,我对java文件做的那些小调整现在允许我在我的app中的页面中导航回来。
以下是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;
}
}
});
答案 1 :(得分: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);
答案 2 :(得分: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
来完成这项工作。