简体   繁体   中英

WebViewClient not called the second time

I have a WebView which I use to load some html content locally in my app. It first loads the content, then calls a JavaScript function which then scrolls the WebView to a particular position.

The following code illustrates how I do this:

public class MyActivity extends Activity {

private WebView web1;
private int ID;
private MyWebViewClient webViewClient1;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity);

        // Get the ID of the law to be loaded.
        ID = getIntent().getIntExtra("element_id", 1);

        web1 = (WebView) findViewById(R.id.web1);
        web1.getSettings().setJavaScriptEnabled(true);

        // Initialize the webViewClients.
        webViewClient1 = new MyWebViewClient(true);

        web1.setWebViewClient(webViewClient1);

        displayArticle(web1);
    }

private void displayArticle (WebView wv) {

StringBuilder sb = new StringBuilder();

// Code to build the HTML String.

String finalHtml = sb.toString();

            wv.loadDataWithBaseURL("file:///android_asset/html/", finalHtml, "text/html", "UTF-8", null);

}

private class MyWebViewClient extends WebViewClient {
        String urlToLoad;

        MyWebViewClient (boolean setUrlToLoad) {
            if (setUrlToLoad) {
                setUrlToLoad();
            }           
        }

        public void setUrlToLoad () {
            this.urlToLoad = "javascript:(function () {" +
                    "var elem = document.getElementById('e"+ID+"');" +
                    "var x = 0;" +
                    "var y = 0;" +
                    "while (elem != null) {" +
                    "x += elem.offsetLeft;" +
                    "y += elem.offsetTop;" +
                    "elem = elem.offsetParent;" +
                    "}" +
                    "window.scrollTo(x, y);" +
                    "})()";
        }

        @Override 
        public void onPageStarted (WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            Log.d("Pages", "Page loading started");
        }

        @Override
        public void onReceivedError (WebView view, int errorCode, String description, String failingUrl) {
            super.onReceivedError(view, errorCode, description, failingUrl);
            Log.d("Pages", "Webview content load error");
        }

        @Override
        public void onPageFinished (WebView view, String url) {
            super.onPageFinished(view, url);
            Log.d("Pages", "Page loading finished");

            if (urlToLoad != null) {
                // Scroll to the position.
                view.loadUrl(urlToLoad);
                urlToLoad = null;
            }
        }
    }
}

In the above code, the callback functions in the MyWebViewClient class are called for the first request using wv.loadDataWithBaseURL in the displayArticle(WebView wv) function, but when the request is finished and onPageFinished is called, the view.loadUrl(urlToLoad); call does not invoke another set of callbacks from MyWebViewClient . I am not quite sure why since it must be the same WebView I originally used and it should have the same instance of MyWebViewClient set.

Moreover, there are other loadUrl calls that I make with the same WebView , and this behaviour persists.

I would really appreciate if someone could explain why this happens.

loadUrl("javascript:...")有点特殊情况:它在当前页面的上下文中评估JavaScript代码(就像<a href='javascript:...'>clicky</a>那样)因此,您将不会获得onPageStarted / onPageFinished回调。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM