Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
505 views
in Technique[技术] by (71.8m points)

android - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...