Android: WebView goback () always reloads the same page for redirect urls

I have implemented my own web browser with basic functionality (with back, forward, refresh and stop buttons). Everything works absolutely fine, but it breaks whenever I load the redirect url into it and try to return by calling 'webview.goBack ()'.

Suppose URL1 redirects to URL2. So when I try to open URL1, the webview is automatically redirected to URL2. After URL2 is fully loaded, if I try to go back to the previous page by calling "webview.goBack", the webview will try to load URL1, which is redirected to URL2 again, and again I come to the same page.

Here is my code: 'private WebViewClient mWebViewClient = new WebViewClient () {

    public void onPageStarted(WebView view, String url,
            android.graphics.Bitmap favicon) {
        Log.d(TAG, "onPageStarted");
    };

    public boolean shouldOverrideUrlLoading(WebView view, String url) {
            Log.d(TAG, "shouldOverrideUrlLoading");
            view.loadUrl(url);
            return true;
    };

    public void onPageFinished(WebView view, String url) {
        Log.d(TAG, "onPageFinished");

    };
};'

      

Any help would be much appreciated. Thank!

+3


source to share


2 answers


try this:



 public boolean shouldOverrideUrlLoading(WebView view, String url) {
            Log.d(TAG, "shouldOverrideUrlLoading");
            return false;
    };

      

+1


source


webView.setInitialScale(1);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setLoadWithOverviewMode(true);

webView.getSettings().setUserAgentString(
"Mozilla/5.0 (Linux; Android 5.1.1; Nexus 5 Build/LMY48B; wv) 
AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 
Chrome/43.0.2357.65 Mobile Safari/537.36"
);

webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setPluginState(WebSettings.PluginState.ON);

webView.setWebViewClient(new WebViewClient() {
                public void onReceivedError(WebView view, int errorCode, 
                String description, String failingUrl) {
                }});


webView.loadUrl(url);

      



0


source







All Articles