Access appView from Cordova 5.0.0

I'm having problems accessing appView from the latest version of Cordoba for Android (5.0.0).

For example, let's say I want to add a Javascript interface to my application. Before this version, I used this line of code:

 super.appView.addJavascriptInterface(new WebAppInterface(this), "jsInterface"); 

And then WebAppInterface :

 public class WebAppInterface { ... } 

Now it just doesn't work. Has Cordoba changed anything recently? I seriously don’t know what to do.

In both cases (previous version and new), my main activity has the following structure:

 public class CordovaApp extends CordovaActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.init(); loadUrl(Config.getStartUrl()); ... } 
+7
java android cordova
source share
2 answers

After several days of searching for a solution, I will finally accept the application to work.

Cordoba has changed the way you access Android webView . Developers using Cordova 5.0.0 and newer should add this line to their core business:

 WebView wV = (WebView)appView.getEngine().getView(); 

And then just call wV, as usual. For example, to add a Javascript interface:

 wV.addJavascriptInterface(new WebAppInterface(this), "jsInterface"); 

I hope this answer helps other people who are confused by this new update.

+19
source share

I was about to give up before I found my answer that helped me solve a related problem - thanks josemmo.

Perhaps this will help others: After upgrading to Cordova 5 / Android 4, I was not able to run the shouldOverrideUrlLoading method of my WebViewClient method, because setting up WebViewClient on a newly created WebView

 WebView webView = new WebView(this); webView.setWebViewClient(new WebViewClient()); 

did nothing on the onCreate method.

Thus, the solution was not to create a new WebView, but to use the cast appView AND mechanism like this:

 SystemWebViewEngine systemWebViewEngine = (SystemWebViewEngine) appView.getEngine(); WebViewClient myWebViewClient = new myWebViewClient(systemWebViewEngine); WebView webView = (WebView) systemWebViewEngine.getView(); webView.setWebViewClient(myWebViewClient); 

Then the custom WebViewClient class needs a constructor:

 public class myWebViewClient extends SystemWebViewClient { public myWebViewClient(SystemWebViewEngine systemWebViewEngine) { super(systemWebViewEngine); } @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { ... } } 

I doubt it should be done that way, but at least his job.

+5
source share

All Articles