Android- Webview onPageFinished Called Twice

后端 未结 7 986
名媛妹妹
名媛妹妹 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: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 
      }
    }
    

提交回复
热议问题