I am creating an Android WebView in the background using the Application Context so that it is loaded and ready when I need to display it. I attach it to my Activity using addView when it is needed. This mostly works great, however when I try to open an HTML select dropdown I get a crash:
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application at android.view.ViewRootImpl.setView(ViewRootImpl.java:540) at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:259) at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69) at android.app.Dialog.show(Dialog.java:286) at com.android.org.chromium.content.browser.input.SelectPopupDialog.show(SelectPopupDialog.java:217) at com.android.org.chromium.content.browser.ContentViewCore.showSelectPopup(ContentViewCore.java:2413) at com.android.org.chromium.base.SystemMessageHandler.nativeDoRunLoopOnce(Native Method) at com.android.org.chromium.base.SystemMessageHandler.handleMessage(SystemMessageHandler.java:27) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5017) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) at dalvik.system.NativeStart.main(Native Method)
I'm assuming this is because I created the WebView with the ApplicationContext. My question is: is there any way to work around this issue? Is there any way to "attach" the existing WebView to a different Activity or Window so that the Dialog can be created? Is there any way to "hack" this together using Reflection by changing the Context at runtime?
EDIT: As suggested below I tested using a MutableContextWrapper and it appears to solve this problem nicely!