2013-12-10 46 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得到這個工作