3

I have implemented my own custom web browser with basic functionalities(with back, forward, refresh and stop buttons). All is working absolutely fine, but it breaks whenever I load redirection url in it, and tries to go back by calling 'webview.goBack()'.

Lets assume that URL1 redirects to URL2. So, whenever I tries to open URL1, webview automatically redirects to URL2. After loading URL2 completely if I tries to go back to previous page by calling 'webview.goBack', webview will try to load URL1, which again redirects to URL2, and again I'm landing on 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 will be highly appreciated. Thank!

Audumbar
  • 404
  • 5
  • 16
  • So where do you expect to reach by clicking back on URL2? I guess URL 1 right? – Darpan Dec 03 '14 at 12:02
  • Then what else do you expect? This URL1 automatically redirects user to URL2, so that is working as expected. You moved to URL2 is the indication that your URL1 has been loaded. You can't stop there, even on desktop browser. – Darpan Dec 03 '14 at 12:37
  • I think you are not getting my problem. See I've not loaded URL2 directly, it got loaded due to rediction in URL1. So, when I say back on webview it should take me to previous page from where I have loaded URL1(and indirectly URL2). – Audumbar Dec 03 '14 at 12:41
  • in your `onBackPressed()`, if current URL is this 'redirected URL`, then you can load, the `URL 0` ie, the url what you were expecting on back pressed. The only hack possible here, so far. – Darpan Dec 09 '14 at 07:20
  • But how to check for redirected URL? How to differentiate between static and redirection URL? – Audumbar Dec 09 '14 at 09:51
  • This is how http://stackoverflow.com/a/5270162/609782 – Darpan Dec 10 '14 at 10:12
  • Thanks @Darpan. But its really a bad idea to make a separate request just to check if its an redirectional URL. – Audumbar Dec 11 '14 at 05:43
  • yes it is, but I couldn't find anything else. – Darpan Dec 11 '14 at 06:42

2 Answers2

4

try this:

 public boolean shouldOverrideUrlLoading(WebView view, String url) {
            Log.d(TAG, "shouldOverrideUrlLoading");
            return false;
    };
Rahul Tiwari
  • 6,851
  • 3
  • 49
  • 78
0
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);
Prashant Gosai
  • 1,186
  • 1
  • 10
  • 18