Tim
Tim

Reputation: 5767

Android WebView

With reference to this WebView tutorial, in particular this method

private void setupWebView(){
    String MAP_URL = "http://gmaps-samples.googlecode.com/svn/trunk/articles-android-webmap/simple-android-map.html";
    String centerURL = "javascript:centerAt(" + mostRecentLocation.getLatitude() + ","+ mostRecentLocation.getLongitude()+ ")";
    webView = (WebView) findViewById(R.id.webview);
    webView.getSettings().setJavaScriptEnabled(true);
    //Wait for the page to load then send the location information
    webView.setWebViewClient(new WebViewClient(){
        @Override
        public void onPageFinished(WebView view, String url){
            webView.loadUrl(centerURL);
        }
    });
    webView.loadUrl(MAP_URL);
}

I've noticed that if I place the webView.loadUrl(centerURL); directly after webView.loadUrl(MAP_URL); like this

private void setupWebView(){
    String MAP_URL = "http://gmaps-samples.googlecode.com/svn/trunk/articles-android-webmap/simple-android-map.html";
    String centerURL = "javascript:centerAt(" + mostRecentLocation.getLatitude() + "," + mostRecentLocation.getLongitude()+ ")";
    webView = (WebView) findViewById(R.id.webview);
    webView.getSettings().setJavaScriptEnabled(true);
    //Wait for the page to load then send the location information
    webView.setWebViewClient(new WebViewClient(){
        @Override
        public void onPageFinished(WebView view, String url){
            //DO NOTHING
        }
    });
    webView.loadUrl(MAP_URL);
    webView.loadUrl(centerURL);
}

it no longer works. So the centreAt(..) javascript method is contained int the MAP_URL.

I'm wondering if the webView.loadUrl(..) method returns before the url has actually been loaded. It looks that way since the top method waits for it to load fully before running the javascript

Upvotes: 2

Views: 8782

Answers (1)

Peter Knego
Peter Knego

Reputation: 80340

Yes, webView.loadUrl() is asynchronous: it returns immediately and WebView keeps working in it's own thread.

To monitor WebView page loading use WebViewClient.onPageFinished(..):

webview.setWebViewClient(new WebViewClient() {
    public void onPageFinished(WebView view, String url) {
        // do something here
    }
});

Upvotes: 14

Related Questions