Phonegap InAppBrowser - back button don't go t

2019-05-04 18:09发布

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.

3条回答
祖国的老花朵
2楼-- · 2019-05-04 18:16

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;
          }
      }
});
查看更多
Rolldiameter
3楼-- · 2019-05-04 18:23

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.

查看更多
一夜七次
4楼-- · 2019-05-04 18:35

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);
查看更多
登录 后发表回答