Search code examples
javaandroidandroid-webviewwebviewclient

Check if certain url is finished loading, then open another one in WebView


I want to check if certain URL is finished loading, and then load another URL and if that new url is finished loading do something, how?

I know how to check if certain URL is finished loading like this:

webView.setWebViewClient(new WebViewClient() {

    public void onPageFinished(WebView view, String url) {
        // do stuff here
    }
    });

But what I want is:

 if url1 finished loading:
    load url2
    if url2 finished loading:
       //do some stuff here

How can I do this?


Solution

  • The url String parameter of onPageFinished always returns the actual URL loaded in the WebView.

    You should do something like this:

    webView.setWebViewClient(new WebViewClient() {
    
    String urlA = "http://facebook.com";
    String urlB = "http://facebook.com/friends";
    
    public void onPageFinished(WebView view, String url) {
        if(url.equals(urlA)){
            //Do stuff if actual page url is urlA        
        }else if(url.equals(urlB)){
            //Do stuff is actual page url is urlB
        }
    }
    });