Encountering an issue with Javascript in WebView. I have a ViewPager that dynamically adds Views as needed. Prior to adding a View to the viewpager, I inflate it and load an embedded webview inside:
LayoutInflater inflater = this.getLayoutInflater();
FrameLayout v = (FrameLayout) inflater.inflate(R.layout.notebook_page, null);
setupWebView(v);
pagerAdapter.addView(v);
pagerAdapter.notifyDataSetChanged();
Within the webview, initially load a local html file, then inject a JS function to set various inputs on the HTML.
private void setupWebView(View v) {
myWebView = (WebView) v.findViewById(R.id.webview);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.loadUrl("file:///android_asset/web_resources/index.html");
myWebView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
super.onPageFinished(myWebView, url);
Log.d("WebView Content", "Injecting JS");
myWebView.loadUrl("javascript:function('" + input_var + "')");
}
});
}
The setupWebView function is being called correctly for each inflated view, but the JS function isn't functioning properly.
This code works flawlessly in an Activity when there's only 1 page. However, within ViewPager where multiple pages display webviews, the JS only loads on the last page.
Any suggestions or recommendations would be greatly appreciated?