24

EDIT: I worked on this project years ago and unfortunately I cannot verify if any of the answers is working in the given scenario.

I am having hard time with one WebView which should show our blog. When initilized, it works just fine. The user can navigate to different links within the WebView. To get back to the blog, there is a button outside the WebView which should load the main blog site again.

The problem is, that nothing is loaded after the second call to loadUrl. Here is my code:

private WebView wv;

        @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

            this.setContentView(R.layout.blog);

    wv = (WebView) findViewById(R.id.blog_webview);
    wv.getSettings().setJavaScriptEnabled(true);
    wv.setWebViewClient(new WebViewClient() {

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon {
            MyLog.logDump("onPageStarted: " + url);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
                            MyLog.logDump("onPageFinished: " + url);
        }
    });

    wv.loadUrl(Constants.BLOG_URL);
}

The function called by my OnClickListener is the following:

    public void reLoadUrl() {
        wv.loadUrl(Constants.BLOG_URL);

}

But despite that the logs in onPageFinished and onPageStarted show that my wv.loadUrl is being invoked and that it's loading the correct url, the content in the webview itself doesn't change. I've tried clearing the cache, the history, the view, tried different WebSettings, tried to use webView.goBack() - not result. Also those ideas don't work: Strange webview goBack issue in android

Sometimes, the reLoadUrl shows the desired result - but once it fails it no longer can be made to work again. Any ideas what could be happening? I did try to read the WebView code but I couldn't find anything that could help me.

The only thing that I can add, is that we are using some ad networks which are heavily dependent on webViews - I tried to turn those down, but I didn't remove the libraries so I am not sure that they are not the culprit.

Any ideas??

Community
  • 1
  • 1
Elena
  • 657
  • 1
  • 7
  • 22
  • Are you getting any warnings at all in your logcat? Maybe some SSL stuff – Stefan de Bruijn Jan 11 '13 at 13:42
  • 1
    While this sounds odd you could try adding ``wv.reload()`` after ``wv.loadUrl(..)``. – harism Jan 11 '13 at 13:43
  • No errors or warnings in the log so far. onReceivedError is not triggered either. Calling reload after loadUrl did not help either. – Elena Jan 11 '13 at 13:53
  • I have also lost my 2 days for this issue. And still no success. Can you please post the answer if you got it – Shirish Herwade Apr 28 '13 at 14:16
  • I found out the exact problem. I have set a web chrome client, whose onJsAlert method gets called when I click submit on first html. And after that webview stops showing other htmls – Shirish Herwade Apr 28 '13 at 16:30
  • In reLoadUrl() method try to load any other site, hot code a site and check whether it is loading or not..? Check that's working or not. If works then make sure your blogurl is not null and not empty. – Santhosh May 02 '13 at 05:27

15 Answers15

11

Use exact in below sequence.

   wv.clearCache(true);
   wv.clearView();
   wv.reload();
   wv.loadUrl("about:blank");
   wv.loadData(Constants.BLOG_URL);
user1621629
  • 765
  • 7
  • 19
8

I have spent the last day or so working on an application that utilised a WebView. I did have a few issues with loading data into the WebView.

I am not entirely sure this would work but perhaps replace this code:

public void reLoadUrl() {
    wv.loadUrl(Constants.BLOG_URL);

}

with this code:

public void reLoadUrl() {

    wv.clearView();
    wv.loadUrl(Constants.BLOG_URL);

}

maybe when you clear the WebView it will solve the issue

Javacadabra
  • 5,578
  • 15
  • 84
  • 152
  • I did try this without success. The webview is cleared but the blog url is not loaded - it just shows a blank page. – Elena Jan 11 '13 at 13:48
  • @Elena did you try what harism said above in comments calling `wv.reload()`? The strange thing is that it works sometimes you say? – Javacadabra Jan 11 '13 at 13:49
  • I did and it didn't help. And yes, sometimes (rarely), the first time when I try to load it again, it works. But almost never the second time and once it fails - it never works again. – Elena Jan 11 '13 at 13:56
  • Hmmm, Sorry I couldn't be of more help. If I can think of anything else in the meantime I will post it otherwise I hope you get it solved! – Javacadabra Jan 11 '13 at 13:57
  • Since "wv.clearView();" has been depricated on Android v18, replacement is " mWebView.loadUrl("about:blank");" – Hesam Dec 05 '13 at 09:49
3

I'd suggest you try recreating the WebView every time you use it and see if that makes any difference.

First save context, layout parms and parent.

Context context = wb.getContext();
ViewGroup.LayoutParams lp = wv.getLayoutParams();
ViewGroup parent = (ViewGroup)wv.getParent();

Then stop it loading and remove the view from its parent.

wv.stopLoading();
parent.removeView(wv);

Then recreate and add it back.

wv = new WebView(context);
parent.addView(wv, lp);

That may be other properties that you'll need to restore, but that's the general idea.

James Holderness
  • 22,721
  • 2
  • 40
  • 52
  • Thank you for the suggestion. Although this solution will probably work, recreating the whole WebView is exactly what I am trying to avoid. – Elena May 07 '13 at 14:37
  • You may be out of luck then. In my experience WebViews in Android are somewhat unreliable. Unless there's a massive performance hit in recreating the WebView every time, if it works, it's better than nothing. At some point you need to just go with whatever lets you move on to your next problem. – James Holderness May 07 '13 at 15:05
  • It works on devices before JellyBean. However, I encounter this problem once I update to JellyBean. Like Shirish Herwade said. I have `onJsAlert` method gets called. It may probably the reason of this problem but I really need this. – Yeung May 24 '13 at 05:23
  • Now I can absolutely confirm that the problem is cause by `onJsAlert `is being called. Once I remove alert call, it work fine. – Yeung May 24 '13 at 06:05
  • If you think you need to override onJsAlert, decide if overriding onConsoleMessage instead would work. Then, the reload has no problem. In my case, I overrode the onJsAlert simply to pass data back. – ZaBlanc Aug 25 '13 at 10:33
3

I had the same issue, calling resumeTimers() solved the problem for me...

Tinsel
  • 96
  • 4
2

Try to load a blank page before:

wv.loadUrl("about:blank");
wv.clearHistory();
wv.clearView();
wv.loadUrl(Constants.BLOG_URL);

Edit: This is a code I used before because I had problems with this too, please try it.

Elad92
  • 2,471
  • 4
  • 22
  • 36
2

Try setting a WebChrome client. The webview needs support of WebChrome Clint to work properly in some cases.

wv.setWebChromeClient(new WebChromeClient());
Sagar Waghmare
  • 4,702
  • 1
  • 19
  • 20
2
@override
public void onFormResubmission(WebView view, Message dontResend, Message resend){
   resend.sendToTarget();
}
Renjith
  • 5,783
  • 9
  • 31
  • 42
Nagaraja
  • 581
  • 1
  • 4
  • 12
2

Just make a method in which you'll flush an reinitialize whole WebView and call it from reLoadUrl().

void loadWebView() {
    wv = (WebView) findViewById(R.id.blog_webview);
    wv.getSettings().setJavaScriptEnabled(true);
    wv.setWebViewClient(new WebViewClient() {

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon {
        MyLog.logDump("onPageStarted: " + url);
    }

    @Override
    public void onPageFinished(WebView view, String url) {
                        MyLog.logDump("onPageFinished: " + url);
    }
});

wv.loadUrl(Constants.BLOG_URL);
}

public void reLoadUrl() {
   loadWebView();
}
bakriOnFire
  • 2,685
  • 1
  • 15
  • 27
1

I am facing the same issue and got it working by following steps:

webView.clearCache(true);
webView.loadUrl("Url");

and I got the multiple url loaded successfully.

Brijesh Thakur
  • 6,768
  • 4
  • 24
  • 29
  • Well at my end , I tried with two different url's and its running successfully. Although I was having the same issue as you – Brijesh Thakur Jul 10 '13 at 14:24
1

I was facing the same problem and finally able to manage to solve this.I am new to android not sure this good or bad but it works for me.I simply clear catch from web view and destroy and re-create before reloading another url .

mWebView.clearCache(true);
// destroy before reload new url
parentView.removeView(mWebView);
mWebView.clearHistory();    
mWebView.destroy();

Though mWebView.destroy(); generate an error "java.lang.Throwable: Error: WebView.destroy() called while still attached!" and i search it but does not get any solution and many say it does not affect application till now(I still does not know solve this N.B: I use WebChromeClient ) .

Engr. Hasanuzzaman Sumon
  • 2,103
  • 3
  • 29
  • 38
0

I was facing the same issue. I was creating a new Webview everytime. Just first destroy your previous webView before creating a new one. I hope this help!! Make your webview a global variable. Like -

Webview w;

then before creating a new webview just destroy the previous one

                if(null != w)
                {
                    //destroy the previous webview, before creating the new one
                    w.destroy();
                }
                w= new WebView(activity);
Agr1909
  • 385
  • 1
  • 5
  • 17
0

Call this method in onPause()

 private void pauseWebView() {
            try {
                Class.forName("android.webkit.WebView")
                        .getMethod("onPause", (Class[]) null)
                        .invoke(webview, (Object[]) null);

            } catch(ClassNotFoundException cnfe) {

            } catch(NoSuchMethodException nsme) {

            } catch(InvocationTargetException ite) {

            } catch (IllegalAccessException iae) {

            }
        }
Dhaiyur
  • 98
  • 9
0

I faced the same issue and none of the above solution worked for me. So I fixed it by reloading WebView using JavaScript as follows:

webView.loadUrl( "javascript:window.location.reload( true )" );

Also JavaScript needs to be enabled for the WebView:

webView.getSettings().setJavaScriptEnabled(true);
N-JOY
  • 10,344
  • 7
  • 51
  • 69
0

Try to reload the url and also properly destroy webview to avoid cache and memory leak issue as I answered here https://stackoverflow.com/a/67450119/1843984

44kksharma
  • 2,740
  • 27
  • 32
-1

In onCreate, instead of using wv.loadUrl(Constants.BLOG_URL); , just use wv.loadDataWithBaseURL(baseUrl, readFileAsString("index.html") , mimeType, "UTF-8", null);

Matthew Haugen
  • 12,916
  • 5
  • 38
  • 54
AshuKingSharma
  • 757
  • 7
  • 20