5

When the page with the WebView first loads, sometimes images are missing or displayed incorrectly. If I reload the page the WebView always displays perfectly. I know everyone will first think I set javascript after loadUrl, but that isn't true.

In onCreate I have:

learnWebView = (WebView)findViewById(R.id.learnWebView);
learnWebView.setWebViewClient(new WebViewClient());
learnWebView.getSettings().setJavaScriptEnabled(true);

Then later in the function called after onCreate I have:

learnWebView.loadUrl("myurl");

And yes, I know that the function with loadUrl is called after onCreate every time.

Adam Johns
  • 35,397
  • 25
  • 123
  • 176
  • 1
    Is this specific to a URL or to an Android version or a particular device? It could be that the WebView is trying to load cached content and fails or something. This happens on desktop WebKit browsers sometimes. Did you try using a fresh cache on first load? – Pierre-Antoine LaFayette Aug 07 '13 at 20:02
  • Actually it seems to be specific to Android 4.0. Doesn't happen on 2.3 or 4.1. By using a fresh cache you mean call learnWebView.clearCache(true)? – Adam Johns Aug 07 '13 at 20:09
  • 1
    Calling clearCache() actually made it worse. It made it so a reload wouldn't fix the issue. – Adam Johns Aug 07 '13 at 20:17
  • 1
    Also you may want to try disabling hardware acceleration for the WebView using learnWebView.setLayerType(View.LAYER_TYPE_SOFTWARE, null) and see if it is a bug in the accelerated WebView. This is assuming you've turned on h/w acceleration on the activity. – Pierre-Antoine LaFayette Aug 07 '13 at 20:18
  • How do I know if I've turned on hardware acceleration on the activity? – Adam Johns Aug 07 '13 at 20:23
  • It's almost acting like the webview didn't have enough time to finish loading the images the first time. Then when I reload it the second time, it gets everything but images from cache and loads all the images. – Adam Johns Aug 07 '13 at 20:26
  • setLayerType didn't help unfortunately, and my min API is also 8 by the way – Adam Johns Aug 07 '13 at 20:27
  • 1
    OK I just wanted to rule out the 4.0+ hardware acceleration issues. I don't have any more specific help for you but you do know now that if your cache is empty the page never loads correctly. Again, I'd consider trying different web pages and seeing if this is specific to a particular site or what and debug from there. Good luck. – Pierre-Antoine LaFayette Aug 07 '13 at 20:41
  • I have the same issue with webpages which should be load twice, it seems that images are cached first time, and displayed second time. – VSB Oct 08 '14 at 20:39
  • @AdamJohns Did you find the solution? – Hieu Rocker Jul 22 '16 at 16:03

3 Answers3

9

Please try this instead of your way, that is a bad practice:

        learnWebView.post(new Runnable() {

            @Override
            public void run() {
                learnWebView.loadUrl("myurl");
            }
        });

Or this, in case the first one wont work:

        learnWebView.postDelayed(new Runnable() {

            @Override
            public void run() {
                learnWebView.loadUrl("myurl");
            }
        }, 500);

Hope this helps.

user2652394
  • 1,686
  • 1
  • 13
  • 15
  • What are the pitfalls of the hack I implemented in comparison with your suggestion? I mean from a practical standpoint what could go wrong with my way that couldn't go wrong with this way? I'll make sure to accept when I verify this works correctly tomorrow. – Adam Johns Aug 08 '13 at 03:02
  • You know, as your way, you let the main thread sleep literally for 1s and reload again (twice), this way is just let the webview execute the job right away (`post()` method) or in the worst case just delay for few milliseconds (`postDelayed()` method) (only once). In case you need an explanation. Cheers! – user2652394 Aug 08 '13 at 03:10
1

Look at onViewAttachedToWindow. You should process your logic in javascript only after onViewAttachedToWindow fired.

falsetru
  • 357,413
  • 63
  • 732
  • 636
Alon
  • 11
  • 1
0

I met the same issue. not sure if it is the same case as yours. I spent more than 3 days and actually found the cause it that another WebView object calls pauseTimers() for saving some CPU performance which actually " Pauses all layout, parsing, and JavaScript timers for all WebViews."

Dangui
  • 101
  • 1
  • 7