2

We're coding an app for Android. It's a WebView that contains Html5 pages. We're using loadUrl() webview's method, in order to push some native OS variables to html, such as:

webview.loadUrl("javascript:myJavascriptFunc('" + myAndroidOSVar + "');");

It works pretty fine. But if we are typing on an input from page while loadUrl() is called, we lose focus of our input fields, even if javascript function called just changes a flag on cache.

Do you know other way to call a Javascript function from WebView instead of loadUrl()?

Iulia Barbu
  • 1,522
  • 12
  • 25
JMixtli
  • 21
  • 1
  • 2

3 Answers3

0

This is the closest you can get to avoiding the issue: WebView hides soft keyboard during loadUrl(), which means a keyboard cannot stay open while calling javascript

Basically you are avoiding the loadUrl call by queuing up commands on the native side and at an interval letting the JS bridge get the commands and execute using JS eval() or something.

Community
  • 1
  • 1
Jasoneer
  • 2,078
  • 2
  • 15
  • 20
0

I'm not sure exactly how it will respond if you have multiple fields in the webview, but I came up with a workaround to keep the keyboard shown: https://stackoverflow.com/a/18776064/513038. It may be worth trying.

Community
  • 1
  • 1
Erhannis
  • 4,256
  • 4
  • 34
  • 48
-1

yes, there is a way by adding a JavaScriptInterface to your WebView , refer this tutorial for more explanation and details

Houcine
  • 24,001
  • 13
  • 56
  • 83
  • We've already used a javascriptInterfaces, but for this particular case, we need to trigger function from webview side (an android timer on our example). What we need is other method than webview.loadUrl() that dont mess with UI, cause loadUrl() set focus to webview. – JMixtli Jan 04 '13 at 18:25