最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Android WebView + loadUrl with javascript + onPageFinished = lag - Stack Overflow

programmeradmin6浏览0评论

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.

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.

Share Improve this question asked Apr 28, 2011 at 14:31 SverSver 3,4096 gold badges35 silver badges53 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 1

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.

I don't think you're going to get an easy solution to this. All that Android can detect (like most browsers) is when the page is fully loaded, which means, all of it is downloaded. Javascript waits for it to be downloaded also, and THEN starts running. It's just the way it works. The only solution I can think of might just be in JQuery. I don't know much about it but there's usually solutions to things like this in it.

发布评论

评论列表(0)

  1. 暂无评论