Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference when I create an Android WebView object with a ContextWrapper and a Context?

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.

like image 740
Ravi Ranjan Avatar asked Mar 08 '13 12:03

Ravi Ranjan


1 Answers

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

like image 191
Jason Polites Avatar answered Sep 28 '22 05:09

Jason Polites