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