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.