Skip to content Skip to sidebar Skip to footer

Android Webview - Navigating Back On Url Redirection

I have a question on the Android webview. Assume URL A redirects to URL B. My android application when it tries to open URL A, webview automatically redirects to URL B. If a URL i

Solution 1:

I have a same problem too, and figured out how to solve it. It's like yours. When I click the first link(www.new.a) it automatically redirects other link(mobile.new.a). Usually the links redirect two or three, and my solution have been worked on almost every redirect links. I hope this answer help you out with annyoing redirecting links.

I finally figured out that. You need a WebViewClient with four APIs. There are shouldOverrideUrlLoading(), onPageStarted(), onPageFinished(), and doUpdateVisitedHistory() in the WebViewClient. All the APIs you need is API 1 so don't worry about.

It goes like this. You can use other function rather than onKeyUp().

publicclassMyWebViewextendsWebView{
    ...
    private int mRedirectedCount=0;
    .... 

    @OverridepublicbooleanonKeyUp(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && this.canGoBack()) {
            if(mRedirectedCount>0){
                while(mRedirectedCount>0){
                    this.goBack();
                    mRedirectedCount--;
                }
                mRedirectedCount=0; //clear
            }else{
                this.goBack();
            }
        returntrue;
    }

    privateclassMyWebViewClinetextendsWebViewClient{
        boolean mIsPageFinished=true;
        ...    

        @OverridepublicbooleanshouldOverrideUrlLoading(WebView view, String url) {
            .....
            if(mIsPageFinished){
                mRedirectedCount=0; //clear count
            }
            .....
        }

        @OverridepublicvoidonPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            mIsPageFinished = false;
        }

        @OverridepublicvoidonPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            mIsPageFinished = true;
        }

        @OverridepublicvoiddoUpdateVisitedHistory(WebView view, String url, boolean isReload) {
            super.doUpdateVisitedHistory(view, url, isReload);

            if(!mIsPageFinished){
                mRedirectedCount++;
            }
        }

Solution 2:

privateclassHelloWebViewClientextendsWebViewClient {
}

mWebView.setWebViewClient(newHelloWebViewClient());

If overriding shouldOverrideUrlLoading(), then return false. May not be correct way but it works for me.

Solution 3:

i have the problem too, now already solved the problem.

solution

**If WebViewClient is provided, return true means the host application handles the url, while return false means the current WebView handles the url. **

Solution 4:

Try to overload the OnBackPressed() to overide the default hard key back action. there u can finish the current activity or control how you want.

Post a Comment for "Android Webview - Navigating Back On Url Redirection"