Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android WebView - navigating back on URL redirection

Tags:

android

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 is being redirected to some other url, I see both these urls are stored in webview history. Now my webview history consists of [, , URL A, URL B ]

On back key click from URL B webpage, webview will try to load URL A, which again redirects to URL B. We need to double click back key to go back beyond URL A

How do I solve this issue ? Struggling from the past 2 hours :(

like image 368
Kumar Avatar asked Aug 10 '11 13:08

Kumar


1 Answers

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().

public class MyWebView extends WebView{
    ...
    private int mRedirectedCount=0;
    .... 

    @Override
    public boolean onKeyUp(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();
            }
        return true;
    }

    private class MyWebViewClinet extends WebViewClient{
        boolean mIsPageFinished=true;
        ...    

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

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

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

        @Override
        public void doUpdateVisitedHistory(WebView view, String url, boolean isReload) {
            super.doUpdateVisitedHistory(view, url, isReload);

            if(!mIsPageFinished){
                mRedirectedCount++;
            }
        }
like image 85
theWook Avatar answered Oct 16 '22 06:10

theWook