Problem : I want to pre load a web page inside an Android WebView and attach it to an Activity when the Activity is ready. The trigger point of loading the webpage is before the actual Activity gets created. So I create a webview object in a service the following way.
MutableContextWrapper contextWrapper = new MutableContextWrapper(serviceContext.getApplicationContext());
this.webView = new WebView(contextWrapper);
When the Activity which needs to show this webview gets created I just create a framelayout and add a child view i.e. this webview and call setContentView. This way my webview shows up preloaded with the webpage. I also do the following
contextWrapper.setBaseContext(CurrentActivity.this);
This is the same ContextWrapper object as specified above. Even after this, the webview doesn't draw Javascript Alerts that it is able to draw in the normal use case.
Even When I do
ContextWrapper contextWrapper = new ContextWrapper(CurrentActivity.this);
this.webView = new WebView(contextWrapper);
this.webView.loadUrl(someUrl);
The WebView is not able to pop up JS alerts. I don't even see any Exception or Fatal tags in the logs. WebView is not able to draw any windows on top of it.
This works fine in the normal way meaning creating a webView directly with current activity's context.
this.webView = new WebView(CurrentActivity.this);
this.webView.loadUrl(someUrl);
For all I know a http://developer.android.com/reference/android/content/ContextWrapper.html just performs the same operations on the base context it holds as passed to it in the constructor.
My use case is something like this. I create a webView object with a http://developer.android.com/reference/android/content/MutableContextWrapper.html passed to it. The MutableContextWrapper holds the context of some activity or it can hold or the application context. When the actual activity which needs to display the webview gets started, I switch the context inside the MutableContextWrapper and display the webview by putting it in a frame layout and setContentView. Basically the trigger for starting to load the page is before the Activity gets created and hence I want to pre load the page in the webview and just attach it to the activity when it gets created.
It looks like you're creating the web view off the main UI thread (based on the use of serviceContext
)
This is generally not a good idea as the web view will expect its internal Handler to be bound to the main UI thread.
There are other threads relating to this:
A WebView in a thread can't be created
You're much better off loading the webview after the activity is loaded (i.e. in the onCreate
method) and simply displaying a "loading" indicator to the user. You can have the webview hidden (with setVisibility
) until it has loaded the page, and use a WebViewClient to listen for the load event.
Once the page has loaded set the visibility on your webview to VISIBLE
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With