Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebView must be loaded twice to load correctly

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.

like image 240
Adam Johns Avatar asked Aug 07 '13 20:08

Adam Johns


2 Answers

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.

like image 200
user2652394 Avatar answered Oct 17 '22 11:10

user2652394


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

like image 44
Alon Avatar answered Oct 17 '22 12:10

Alon