Open Craigstock opened 8 years ago
We have worked hard to try and fix this problem but we're really struggling with Cordova 5.x. For the moment we would suggest using Cordova 4.x
I got this working for me with Cordova 5.4.1 and cordova android platform 4.1.1
root of the problem: CordovaWebView has become an interface instead of a class in cordova android 4+
My changes to CordovaFragment.java: onCreateView() needs to be rewritten
protected CordovaPreferences preferences;
protected String launchUrl;
protected ArrayList<PluginEntry> pluginEntries;
protected CordovaInterfaceImpl cordovaInterface;
protected void loadConfig() {
ConfigXmlParser parser = new ConfigXmlParser();
parser.parse(getActivity());
preferences = parser.getPreferences();
preferences.setPreferencesBundle(getActivity().getIntent().getExtras());
// preferences.copyIntoIntentExtras(getActivity());
launchUrl = parser.getLaunchUrl();
pluginEntries = parser.getPluginEntries();
// Config.parser = parser;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
LayoutInflater localInflater = inflater.cloneInContext(new CordovaContext(getActivity(), this));
View rootView = localInflater.inflate(R.layout.fragment_cordova, container, false);
cordovaInterface = new CordovaInterfaceImpl(getActivity());
if(savedInstanceState != null) cordovaInterface.restoreInstanceState(savedInstanceState);
loadConfig();
myWebView = new CordovaWebViewImpl(CordovaWebViewImpl.createEngine(getActivity(), preferences));
myWebView.getView().setId(100);
RelativeLayout.LayoutParams wvlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
myWebView.getView().setLayoutParams(wvlp);
myWebView.getView().setBackgroundColor(Color.argb(1, 0, 0, 0));
// fixes a bug in android 3.0 - 4.0.3 that causes an issue with transparent webviews.
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB
&& android.os.Build.VERSION.SDK_INT <= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
myWebView.getView().setLayerType(WebView.LAYER_TYPE_SOFTWARE, null);
}
if (! myWebView.isInitialized()) {
myWebView.init(cordovaInterface, pluginEntries, preferences);
}
cordovaInterface.onCordovaInit(myWebView.getPluginManager());
// Config.init(getActivity());
((RelativeLayout)rootView).addView(myWebView.getView());
myWebView.loadUrl(launchUrl);
return rootView;
}
and overwrite the onDestroy() function as well.
public void onDestroy() {
super.onDestroy();
if (myWebView.getPluginManager() != null) {
myWebView.getPluginManager().onDestroy();
}
}
And remove the CordovaWebView tag from fragment_cordova.xml. frament_cordova.xml ends up looking like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="0dp">
</RelativeLayout>
Disclaimer: This works in the app i'm currently working on. I haven't tested this with a clean cordova install and the Demo app yet. I'm also not a Java-developer. I still need to do some further testing, but the basic operations seem to work. (open, sync, scan, dismiss)
I hope this works for other people as well :)
Courtesy to this stackoverflow post that got me started: http://stackoverflow.com/questions/30489176/cordova-webview-inside-android-fragment-on-cordova-4-0
Wow Thomas, this is amazing. Thank you so much for taking the time and sharing your fix.
We will be testing it, and seeing if there's a neat way to retain backwards compatibility. Expect an update to the main project in January.
Hello @thomasuvyn
Thank for have sharing your fix, it help me to fix a runtime error I got in this issue https://github.com/thomasforth/MS4Plugin/issues/8#issuecomment-208818763
Hi All, is there an official fix to this problem yet? I have upgraded to Cordova ver 4.1.1 for Android Platform and added the changes above, all is fine except for "CordovaWebViewImpl cannot be resolved to a type."
As follows:
Any ideas?
In case this helps anyone else who has the same problem, I resolved the issue by adding in the import: “import org.apache.cordova.CordovaWebViewImpl;” to the CordovaFragment.java file. The app now runs as expected.
I have an issue with the Android install. I have followed your example video to the letter, but I’m still getting an error. I have attached a screen shot to see if you have any ideas around this?
It is having issues with the cordovafragment.java – I’m not sure if this is due to a new Android API, or the newer cordova version?
Attempting this in Eclipse Mars 1, with Android 5.1.1 and the latest Cordova.
Herewith a screenshot:
Any help would be greatly appreciated!