-1

I am adding content to a webview then running some javascript on the page. The script does work but it doesnt work the first time i load the webview. If I load the webview the very first time in the app, it doesnt change the content. The second time I load the webview (ie go back then click the same list item) the javascript will work and change the content for me.

here is my set up, the javascript that doesnt work on the first load but does on the second is in the onPostExecute of the AsyncTask

IN onCreateView

WebSettings webSettings = mWebView.getSettings();

webSettings.setJavaScriptEnabled(true);
mWebView.setWebViewClient(new WebViewFragmentClient());
mMainLayout.setBackgroundColor(MainActivity.mSettings.isNightMode() ? Color.DKGRAY : Color.WHITE);

WebViewClient to captuer the page load completion then run the JS and show the view

private class WebViewFragmentClient extends WebViewClient {

    @Override
    public void onPageFinished(WebView view, String url) {
        // Page is loaded update the contents textsize and mode and set it visible
        view.loadUrl(String.format("javascript:(document.body.className = '%s');", MainActivity.mSettings.isNightMode() ? "night" : "day"));
        view.loadUrl(String.format("javascript:(document.body.style.fontSize ='%spt');", MainActivity.mSettings.getTextSize()));
        view.setVisibility(View.VISIBLE);

        super.onPageFinished(view, url);
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {         
        return mCallBack.pageLinkSelected(url);
    }
}

ASYNC Load OF DATA

private class AsyncLoadContent extends AsyncTask<Void, Void, Void> {
    String mContent;

    @Override
    protected void onPreExecute() {
        mWebView.setVisibility(View.INVISIBLE);
        super.onPreExecute();
    }

    @Override
    protected Void doInBackground(Void... params) {
        // Set the content of the page
        mContent = mCallBack.getContent();
        return null;
    }

    @Override
    protected void onPostExecute(Void result)
    {
        mWebView.loadDataWithBaseURL("file:///android_asset/", mContent, "text/html", "utf-8", null);
    }
}

Thanks, DMan

DMCApps
  • 2,110
  • 4
  • 20
  • 36

1 Answers1

1

Do not use AsyncTask, WebView handles page loading itself. loadDataWithBaseURL is async method and returns immediately. Use WebViewClient to listen for page loading completed.

Community
  • 1
  • 1
Leonidos
  • 10,482
  • 2
  • 28
  • 37
  • I am using an async task because the content creation is relatively heavy and I don't want to freeze the UI during this time. The script still runs on subsequent loads of any content so why is it that the async task is messing this up? What is the method to catch the load completion event? – DMCApps Jan 10 '13 at 15:20
  • Made changes to reflect what you have said and it seems to be working perfectly. Code has been updated in the question thanks a lot. Seems like something with the synchronization was off since the content does load asyncly. Thanks – DMCApps Jan 10 '13 at 15:28