javascriptandroidwebview

Android WebView + loadUrl with javascript + onPageFinished = lag


I have WebView which loads some page, when it's finished I apply some javascript magic to mess up with DOM. Everything is fine, page loads and onPageFinished I just call wv.loadUrl(javascript);

But I don't want to see loading process, and how javascript is working, I just need result, so I made my view invisible with wv.setVisibility(View.INVISIBLE); from the start, and make it visible again when everything is done. This is where problem occurs.

This piece of code should make view visible after javascript is finished, but wv.setVisibility(View.VISIBLE) fires before javascript. So for a moment I see page and how it is being changed by javascript. This is just ugly.

  public void onPageFinished (WebView view, String url) {
         wv.loadUrl(javascript);
         view.getSettings().setLoadsImagesAutomatically(true);
         wv.setVisibility(View.VISIBLE);

    }

I got that loadUrl works asynchronously, so I tried to make another WebViewClient with just "make first view visible" method inside onPageFinished, and use it to call JS. But it just keeps crashing with NPE error.

wv2.setWebViewClient(new WebViewClient() {
    @Override
    public void onPageFinished (WebView view, String url) {
     wv.setVisibility(View.VISIBLE);
}
});

For now I just added delay after javascript (SystemClock.sleep(5000)), but this is like.. yeah.


Solution

  • Ok, I think I figured it out. We can have callback from the end of javascript that will fire "show view" - like here or here (with handler, because I wanted to update ui). But it wont change anything, the problem seems to be in rendering speed. It's just slow. So for now I'll just stick with 1 second delay after firing JS.