Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android- Webview onPageFinished Called Twice

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 twice for some reason, which really messes up my application. Here's the code:

public class WebViewActivity extends Activity {
private WebView gWebView;
final String REDIRECT_URI = "https://localhost:5000/receive_code";
final String CLIENT_ID = "can't post it here";
final String CLIENT_SECRET = "can't post it here";
final String SCOPE = "basic names genomes analyses";
Hashtable<String, String> riskPairs;

public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.webview);

    gWebView = (WebView) findViewById(R.id.webView1);

    gWebView.loadUrl("https://api.23andme.com/authorize/?redirect_uri="
            + REDIRECT_URI + "&response_type=code&client_id=" + CLIENT_ID
            + "&scope=" + SCOPE);

    Log.d("WEBVIEW", "got to webpage");

    gWebView.setWebViewClient(new WebViewClient() {

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            if (url.startsWith(REDIRECT_URI)) {
                Log.d("WEBVIEW", "onpagefinished is called");
                System.out.println("got to override");
                if (url.indexOf("code=") != -1) {
                    //if the query contains code
                    String queryString = null;
                    try {
                        queryString = new URL(url).getQuery();
                    } catch (MalformedURLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    System.out.println(queryString);
                    String[] params = queryString.split("&");
                    String code = null;
                    for (String param : params) {
                        if (param.startsWith("code=")) {
                            code = param.substring(param.indexOf('=') + 1);
                        }
                    }
                    gWebView.setVisibility(View.GONE);
                    new PostRequest().execute(code);
                    // don't go to redirectUri
                }
            }
        }
    });


}
class PostRequest extends AsyncTask<String,Void,String>{ code getting client data...}

P.S. Please don't mark this as a duplicate...I've read a similar question on StackOverflow and calling ShouldOverrideUrlLoading does not work for me(which is why I used onPageFinished() in the first place).

like image 711
StephenChen Avatar asked Aug 16 '13 22:08

StephenChen


6 Answers

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 
  }
}
like image 105
Samvel Kartashyan Avatar answered Nov 12 '22 13:11

Samvel Kartashyan


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.

like image 30
thegr8 Avatar answered Nov 12 '22 14:11

thegr8


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);
}
like image 8
Balazs F. Avatar answered Nov 12 '22 13:11

Balazs F.


I'm looking into an event where m.yotube.com triggers two onPageFinished events but it does not seem like caused by redirection to me. After some studies I found that there is one extra onPageFinished triggered by didFinishNavigation before the one triggered by didStopLoading which other pages also receives.

stack trace #1stack trace #2

See also:

https://chromium.googlesource.com/chromium/src.git/+/master/android_webview/java/src/org/chromium/android_webview/AwWebContentsObserver.java

    @Override
    public void didFinishNavigation(final String url, boolean isInMainFrame, boolean isErrorPage,
            boolean hasCommitted, boolean isSameDocument, boolean isFragmentNavigation,
            Integer pageTransition, int errorCode, String errorDescription, int httpStatusCode) {
        ...
        if (client != null && isFragmentNavigation) {
            client.getCallbackHelper().postOnPageFinished(url);
        }
    }

    @Override
    public void didStopLoading(String validatedUrl) {
        if (validatedUrl.length() == 0) validatedUrl = ContentUrlConstants.ABOUT_BLANK_DISPLAY_URL;
            AwContentsClient client = getClientIfNeedToFireCallback(validatedUrl);
        if (client != null && validatedUrl.equals(mLastDidFinishLoadUrl)) {
            client.getCallbackHelper().postOnPageFinished(validatedUrl);
        mLastDidFinishLoadUrl = null;
        }
    }

Another instance I receive extra onPageFinished calls (even before onPageStarted!) is when I utilize webview.restoreState() in Fragments. it fires two onPageFinished events when trying to resume last viewed page.

like image 3
Teng-pao Yu Avatar answered Nov 12 '22 15:11

Teng-pao Yu


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.

like image 2
StephenChen Avatar answered Nov 12 '22 13:11

StephenChen


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);
    }
like image 1
Abhishek Avatar answered Nov 12 '22 14:11

Abhishek