For up-to-date product documentation, see the IBM MobileFirst Foundation Developer Center.
Cordova application with MobileFirst start-up flow
In Android Studio, we can review the start-up process of the Cordova app for Android with MobileFirst.
The MobileFirst Cordova plug-in cordova-plugin-mfp has native asynchronous bootstrap sequence. The bootstrap sequence must be completed before the Cordova application loads the application's main html file.
Add the cordova-plugin-mfp plug-in to a Cordova application instruments the application's AndroidManifest file and the MainActivity extending the CordovaActivity native code to perform the MobileFirst initialization. MobileFirst can also be used in Cordova applications configured with the Crosswalk WebView.
The application native code instrumentation consists of:
- Add com.worklight.androidgap.api.WL API calls to perform the MobileFirst initialization.
- In the AndroidManifest.xml file adding
- An activity called MFPLoadUrlActivity to allow proper MobileFirst initialization in case the cordova-plugin-crosswalk-webview has been installed.
- A custom attribute android:name="com.ibm.MFPApplication" to the <application> element (see below).
Implementing WLInitWebFrameworkListener and creating the WL object
The MainActivity.java file creates the initial MainActivity class extending the CordovaActivity class. The WLInitWebFrameworkListener receives notification when the MobileFirst framework is initialized.
public class MainActivity extends CordovaActivity implements WLInitWebFrameworkListener {
The MFPApplication class is called from within onCreate and creates a MobileFirst client instance (com.worklight.androidgap.api.WL) that is used throughout the app. The onCreate method initializes the WebView framework.
@Overridepublic void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); if (!((MFPApplication)this.getApplication()).hasCordovaSplashscreen()) { WL.getInstance().showSplashScreen(this); } init(); WL.getInstance().initializeWebFramework(getApplicationContext(), this); }
The MFPApplication class has two functions:
- Defines the showSplashScreen method for loading a splash screen if one exists. For more information on controlling splash screens see c_splash_screen.html.
- Creates two listeners for enabling analytics. These listeners can be removed if not needed. For more information on Cordova and MobileFirst analytics see MobileFirst Cordova plug-in initialization for analytics.
Loading the WebView
The cordova-plugin-mfp plug-in adds an activity to the AndroidManifest.xml file that is required for initializing the Crosswalks WebView:
<activity android:name="com.ibm.MFPLoadUrlActivity" />.
This activity is used to ensure the asynchronous initialization of the Crosswalk WebView as follows:
After the MobileFirst framework is initialized and ready to load in the WebView, the onInitWebFrameworkComplete connects to the URL if WLInitWebFrameworkResult succeeds.
public void onInitWebFrameworkComplete(WLInitWebFrameworkResult result){ if (result.getStatusCode() == WLInitWebFrameworkResult.SUCCESS) { super.loadUrl(WL.getInstance().getMainHtmlFilePath()); } else { handleWebFrameworkInitFailure(result); } }
For details on the WebView development see Cordova WebView.
Parent topic: Develop Cordova apps for Android