Android WebView - navigating back on URL redirecti

2020-02-05 03:20发布

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 :(

标签: android
4条回答
Lonely孤独者°
2楼-- · 2020-02-05 03:47
private class HelloWebViewClient extends WebViewClient {
}

mWebView.setWebViewClient(new HelloWebViewClient());

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

查看更多
爱情/是我丢掉的垃圾
3楼-- · 2020-02-05 03:52

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. **

查看更多
女痞
4楼-- · 2020-02-05 04:06

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.

查看更多
家丑人穷心不美
5楼-- · 2020-02-05 04:08

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++;
            }
        }
查看更多
登录 后发表回答