Can I change the context of a WebView after it is created?

I have a WebView I load into the action so that it is preloaded so that it pops up immediately in another Activity (started from the first).

The problem is that in order to create an instance of a WebView , I have to go into Context , in which case this is the first one mentioned above.

This way it works great, and the second Activity shows the WebView just fine. The problem is that if I clicked the <select> drop-down list in the WebView , its selection dialog will appear under the WebView. It seems that the selection does not work at all until you press the back button and see a short selection dialog before returning to parent activity.

It seems that when I add the WebView to the layout in the second step, the modalities snap to this activity window, but the WebView itself WebView bound to the parent activity window, so it points to the highest point in the hierarchy.

How can I change Context WebView after creating it?

This is a very difficult problem to solve - I need to create a WebView before starting, but I also need selection dialogs.

Please, if anyone can give me some ideas, I would really appreciate it.

This is for the SDK project, so I will not have access to parent activity. In addition, saveState does not work, because the bulk of what is displayed in the WebView is generated by JavaScript, and the full DOM stack is not passed.

+7
android webview
source share
1 answer

You can try creating a WebView with a MutableContextWrapper :

 MutableContextWrapper mMutableContext=new MutableContextWrapper(context); WebView mWebView=new WebView(mMutableContext); 

and later you could do

 mMutableContext.setBaseContext(newcontext); 

But...

  • WebView is a very complex component that is likely to use the passed context to create other objects, such as Handlers. WebView probably uses these handlers to publish material to the original user interface stream, so in the end you will probably have a combination of contexts, as you know, with a double memory leak (if it ever works correctly)

    / li>
  • Webview covers at least 1 "webcore" stream, where the action takes place, as well as constant communication with the original user interface stream with ... handlers? through the source context? who knows!

  • There are even 2 different mechanisms for viewing web pages: Kitkat is based on chrome, and jelly bean, and in previous versions AOSP / WebView is used. So you have an extra breakpoint.

  • The reasons you state are not strong enough. WebView is not so slow. If the application is loading, try optimizing it. There are many things you can do for this: loading HTML and graphics from internal assets.

+11
source share

All Articles