2012-11-12 33 views
0

我在我的應用程序中有一個webview,首先它加載a.html,單擊a.html中的一個按鈕,然後b.html將被加載,單擊b.html中的一個按鈕,然後一個活動將開始。總之,訂單是a.html-> b.html->開始活動。我的webView已經擴展了WebViewClient,並覆蓋了它的方法如下。何時調用WebViewClient的方法?

private class WebViewHandler extends WebViewClient 
    { 
     @Override 
     public void onPageStarted(WebView view, String url, Bitmap favicon) 
     { 
      Log.d("onPageStarted", "onPageStarted:" + url); 
      mProgress.setVisibility(View.VISIBLE); 
     } 

     @Override 
     public void onPageFinished(WebView view, String url) 
     { 
      Log.d("onPageFinished", "onPageFinished:" + url); 
      mProgress.setVisibility(View.GONE); 
     } 

     @Override 
     public boolean shouldOverrideUrlLoading(WebView view, String url) 
     { 
      Log.d("url", "onPageoverloaded the url: "+url); 
      String tutorialId = url.substring(url.lastIndexOf("=") + 1); 
      MetaioDebug.log("Tutorial Id detected: "+tutorialId); 
      if (url.startsWith("metaio://")) 
      { 
       if (tutorialId != null) 
       { 
        MetaioDebug.log("Native code tutorial to be loaded #"+tutorialId); 
        if (tutorialId.equals("1")) 
        { 
         Intent intent = new Intent(getApplicationContext(), Tutorial1.class); 
         startActivity(intent); 
        } 

       return true; 
      } 
    } 

問題是,當a.html開始加載,但是當b.html開始加載不會被調用onPageStarted()只調用。 shouldOverrideUrlLoading(WebView view, String url)僅在單擊b.html中的按鈕但不在a.html中時纔會調用。

我很困惑什麼時候應該調用這3種方法?

回答

0

我認爲onPageFinished()應該可能返回false

documentation for shouldOverrideUrlLoading()

If WebViewClient is provided, return true means the host application handles the url, while return false means the current WebView handles the url.

不知道爲什麼它不a.html工作,但因爲如果它會系統會在某個時候決定它可能與你的回報價值處理事情本身在前進。

至於你的onPageStarted問題,我認爲你正在看到預期的行爲。 Documentation for onPageStarted():

Notify the host application that a page has started loading. This method is called once for each main frame load so a page with iframes or framesets will call onPageStarted one time for the main frame. This also means that onPageStarted will not be called when the contents of an embedded frame changes, i.e. clicking a link whose target is an iframe.

我覺得你的WebView認爲自己處於「結束」狀態的,因爲它已經完成了原始的渲染你已經要求它從a.html做。

解決方法是創建一個新的帶有b.html的WebView並使其膨脹,以保證onPageFinished()被調用。我認爲這取決於你如何在webview中加載b.html。

您是否暗示您在加載b.html後調用onPageFinished(),而不是onPageStarted()?或者不要打電話?

添加基於評論:

試裝b.html另一種方式來讓web視圖瞭解它加載了全新的資源:

有其電流值hrefb.html導致一個JavaScript方法,該方法調用返回到JavaScript中調用類似於Java的JavaScript接口對象:

webview.loadURL("file:///b.html"); //or whatever the file location of b.html is.

我想這會讓你的WebView認爲它正在加載一個新的資源,所以請撥打onPageStarted()onPageFinished()

+0

對於最後一個問題:是的,在加載b.html之後調用onPageFinished(),但在啓動加載b.html時不調用onPageStarted()。順序應該是onPageStarted() - > onPageFinished(),但這不是在這裏發生的事情。 – Blake

+0

'onPageFinished()'與'onPageStarted()'沒有相同的註釋,它只被調用一次。對我來說,這表明它會在每次(重新)加載資源時被調用。不幸的是,我認爲你正在看到預期的行爲。順便說一句,你是如何加載'b.html'?和「a.html」一樣嗎? – Jon

+0

你的意思是「onPageFinished()與onPageStarted()沒有相同的註釋」?在a.html中有這樣一行''何時加載b.html – Blake

相關問題