So the hardest part is what??? It’s the fact that your CordovaActivity inside the aar can’t be invoked from the client app. So, this tutorial isn’t complete until we are able to call CordovaActivity from outside the aar.
I am not going to spend time showing you that it can’t be invoked. But instead I’ll show you how to rewrite the CordovaActivity to Activity class.
If you notice the MainActivity.java in src>main>java>io>ionic>starter, the MainActivity class implements the CordovaActivity class.
To resolve this simply copy paste my Activity class below and paste it to replace your MainActivity.java:-
package io.ionic.starter;
import android.app.ActivityManager;
import android.app.ActivityOptions;
import android.app.AlertDialog;
import android.os.Bundle;
import org.apache.cordova.*;
import android.app.Activity;
import android.content.Context;
import android.view.ViewGroup;
import android.graphics.Color;
import android.content.Intent;
import java.util.ArrayList;
import java.util.Locale;
import android.media.AudioManager;
import android.widget.FrameLayout;
import android.net.Uri;
import android.webkit.WebSettings;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.CompletableFuture;
import org.json.JSONObject;
import android.os.AsyncTask;
public class MainActivity extends Activity
{
protected CordovaPreferences preferences;
protected CordovaWebView appView;
protected String launchUrl;
protected ArrayList<PluginEntry> pluginEntries;
protected CordovaInterfaceImpl cordovaInterface;
public static String screenRender = "/details";
private static Context context;
public boolean setScreenRender(String screenname) {
screenRender = screenname;
return true;
}
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// enable Cordova apps to be started in the background
Bundle extras = getIntent().getExtras();
if (extras != null) {
this.screenRender=extras.getString("KEY");
System.out.println("KEY:::::" + extras.getString("KEY"));
}
this.loadConfig();
String logLevel = preferences.getString("loglevel", "ERROR");
cordovaInterface = makeCordovaInterface();
if (savedInstanceState != null) {
cordovaInterface.restoreInstanceState(savedInstanceState);
}
//to implement async call here for intent received
System.out.println("Received intent to render:::::::" + screenRender);
class mATask extends AsyncTask<String, String, String>
{
protected void onPreExecute() {
super.onPreExecute();
Bundle extras = getIntent().getExtras();
if (extras != null) {
screenRender=extras.getString("ROOT");
System.out.println("root:::::" + extras.getString("ROOT"));
}
}
@Override
protected String doInBackground(String... params) {
//Enter code for fetching value from remote server
return null;
}
@Override
protected void onPostExecute(String result) {
loadUrl(launchUrl);//Enter code for parsing and create list view
}
};
new mATask().execute("","","");
//CompletableFuture.supplyAsync(this::getIntentExtras)
// .thenAccept(loadUrl(launchUrl));
}
// Author: Kok Seng, Chua
// here are all the implementations of CordovaActivity to run the loadUrl
private void getIntentExtras(){
Bundle extrasMsg = getIntent().getExtras();
if (extrasMsg != null) {
this.screenRender=extrasMsg.getString("ROOT");
System.out.println("root:::::" + extrasMsg.getString("ROOT"));
}
}
@SuppressWarnings("deprecation")
protected void loadConfig() {
ConfigXmlParser parser = new ConfigXmlParser();
parser.parse(this);
preferences = parser.getPreferences();
preferences.setPreferencesBundle(getIntent().getExtras());
launchUrl = parser.getLaunchUrl();
pluginEntries = parser.getPluginEntries();
//Config.parser = parser;
}
public void loadUrl(String url) {
if (appView == null) {
init();
}
appView.loadUrlIntoView(url, true);
}
protected void init() {
appView = makeWebView();
createViews();
if (!appView.isInitialized()) {
System.out.println(pluginEntries);
try {appView.init(cordovaInterface, pluginEntries, preferences);}
catch (Exception e){}
}
cordovaInterface.onCordovaInit(appView.getPluginManager());
// Wire the hardware volume controls to control media if desired.
String volumePref = preferences.getString("DefaultVolumeStream", "");
if ("media".equals(volumePref.toLowerCase(Locale.ENGLISH))) {
setVolumeControlStream(AudioManager.STREAM_MUSIC);
}
}
protected CordovaWebView makeWebView() {
return new CordovaWebViewImpl(makeWebViewEngine());
}
protected CordovaWebViewEngine makeWebViewEngine() {
return CordovaWebViewImpl.createEngine(this, preferences);
}
@SuppressWarnings({"deprecation", "ResourceType"})
protected void createViews() {
appView.getView().setId(100);
appView.getView().setLayoutParams(new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
setContentView(appView.getView());
if (preferences.contains("BackgroundColor")) {
try {
int backgroundColor = preferences.getInteger("BackgroundColor", Color.BLUE);
// Background of activity:
appView.getView().setBackgroundColor(backgroundColor);
}
catch (NumberFormatException e){
e.printStackTrace();
}
}
appView.getView().requestFocusFromTouch();
}
protected CordovaInterfaceImpl makeCordovaInterface() {
return new CordovaInterfaceImpl(this) {
@Override
public Object onMessage(String id, Object data) {
// Plumb this to CordovaActivity.onMessage for backwards compatibility
//return KokSengActivityHandler.this.onMessage(id, data);
return super.onMessage(id,data);
}
};
}
}
There you have it….now make your module…
And as promised you can call your application from another app. Simply go to build>output>aar and copy the aar file.
Create your new client app and add the aar file to your client app’s libs folder. If it doesn’t have one yet, create a “libs” folder under app directory. To invoke the activity simply paste the following in your new client app:-
Intent intent = intent = new Intent(this, io.ionic.starter.MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
this.startActivity(intent);
There it is Part 2 completed….if you run your client app, it should start the ionic screens you have created in Part 1.
Original tutorial by KS Chua. Thanks for learning with me.


