Android- Webview onPageFinished Called Twice

后端 未结 7 980
名媛妹妹
名媛妹妹 2020-12-05 14:08

I have an activity that does OAuth authentication by intercepting the redirect url once it show up in the webview. However, the onPageFinished function is somehow called twi

相关标签:
7条回答
  • 2020-12-05 14:22

    Maybe it helps someone.

    I have the some problem - method onPageFinished called twice.

    webView.getProgress();

    at the first execution webView.getProgress() == 89, and at the second == 100. 100 means page loading complete.

    0 讨论(0)
  • 2020-12-05 14:25

    If the url is OK after onPageStarted method starts onPageFinished, but if the url is redirected after onPageStarted starts shouldOverrideUrlLoading and then onPageFinished. You should just check if the loading URL is redirected or not

    private boolean isRedirected;
    
    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {   
    
      if (!isRedirected) {      
        //Do something you want when starts loading
      }
    
      isRedirected = false;
    }
    

    If the URL is redirected the callback starts this function

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
    
      view.loadUrl(url);
      isRedirected = true;
      return true;
    }
    

    Before doing something in onPageFinished check if the callback has entered into shouldOverrideUrlLoading method

    @Override
    public void onPageFinished(WebView view, String url) {
    
      if (!isRedirected) {
        //Do something you want when finished loading 
      }
    }
    
    0 讨论(0)
  • 2020-12-05 14:25

    This trick helps me(Not Recommended But Helps)

    private boolean alreadyEvaluated = false;
    
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
    
            Logger.d(TAG, "onPageStarted");
    
            super.onPageStarted(view, url, favicon);
        }
    
        @Override
        public void onPageFinished(WebView view, String url) {
    
            Logger.d(TAG, "onPageFinished");
    
            if (!alreadyEvaluated) {
                alreadyEvaluated = true;
                view.loadUrl("javascript:window.MyJavaScript.getPageText(document.getElementsByTagName('body')[0].innerText);");
            } else {
                alreadyEvaluated = false;
            }
    
            super.onPageFinished(view, url);
        }
    
    0 讨论(0)
  • 2020-12-05 14:31

    Android for some reason calls onPageFinished() twice(and onPageStarted() three times!) when the loaded url is not a working one. The temporary solution is changing the redirect_uri to the url of a working website; in this case, I changed it to https://www.google.com/ (lol, sorry Google). onPageFinished is then only called once.

    BUT- I do still want answers on why webview behaves differently when the loaded url is not a working one, and what is a better solution than changing the redirect_uri to google.com.

    0 讨论(0)
  • 2020-12-05 14:32

    So, none of the above answers gave me a solution, because my problem wasn't redirecting, the webview simply called onPageFinished 3 times on the same webpage. My solution is as below, the main idea is to perform your functions on the same url only once.

    private String urlFinished = " ";
    
    @Override
    public void onPageFinished(WebView view, String url) {
    
        Logger.d(TAG, "onPageFinished");
    
        if (!urlFinished.equals(url)) {
          //Place your code here
        }
    
        urlFinished = url;
    
        super.onPageFinished(view, url);
    }
    
    0 讨论(0)
  • 2020-12-05 14:40

    Since onPageFinished is being called more than once, you can simply check if the method is already called before executing anything further.

    @Override
    public void onPageFinished(WebView view, String url) {
         if (loaded) {
              return;
         }
    
         // Do something...
    
         loaded = true;
    }
    
    0 讨论(0)
提交回复
热议问题