Phonegap InAppBrowser - back button don't go to previous page

倖福魔咒の 提交于 2019-12-22 08:07:08

问题


I'm using Phonegap for my application and I need to display an external link in a InAppBrowser but it looks like the back botton is not working as expecting: if I'm doing

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

from from a.html page I clicked a link to www.example.com/b.html next time when I click back, the InAppBrowser is closed but it should go back to a.html.

Do you know how can I enable the 'navigation history' for InAppBrowser?

Thank you.


回答1:


This is possible by tweaking the 'InAppBrowser.java'. I know this is little weird but this is the only choice I had. But, those little tweaks I made to the java file now allows me to navigate back within the pages in my app.

Here is the change to be made in InAppBrowser.java, In the 'run' method within showWebPage method, there will be a listener code something like this:

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

Below that line add the below code,

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



回答2:


You could listen for the back button (assuming you're on android) and inject the history.back call into the 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:


In addition to what @Suresh Raja wrote, the referenced code doesn't exist anymore. you can add the suggested improved code (following) after this peace of code:

dialog.setInAppBroswer(getInAppBrowser());

suggested improved code:

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

this will close the application upon the last back press (which can solve another issue with the inAppBrowser. Hope that helps

EDIT: you should add the import android.content.DialogInterface to get this work.



来源:https://stackoverflow.com/questions/20497317/phonegap-inappbrowser-back-button-dont-go-to-previous-page

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