Geek Culture
Published in

Geek Culture

How to pass params on App Switch from Cordova to KOTLIN App

So recenty i had a task where i needed to push some params from one app to another app during app switch via intent and the KOTLIN app was supposed to grab these and do some specific actions based on if the params were passed or not.

It took me some researching and finding out the right plugins to finally be able to do so. I would like to condense my learnings into this article so that anyone else wanting to do the same might not have to struggle a lot.

Cordova App Implementation — SOURCE

You will need to use some plugin here to enable passing data into intent. For my purposes I went ahead with using link

cordova-plugin-app-launcher

There are multiple ways of triggering the destination app now using this plugin.

This plugin allows you to check if an app is installed that can handle a specific uri and launch an app via uri on iOS and Android. Additionally, you may open an Android app using its package id.

(iOS, Android) Check if any apps are installed that can launch via a specified uri.(iOS, Android) Launch an app via a specified uri.

(Android) Check if an app is installed via its package id.

(Android) Check if an app is installed for an action name.

(Android) Launch an app via its package id.

(Android) Launch an app for an action name.

(Android) Launch an app with extras included.

(Android) Return results from a launched app once it is finished.

I chose the way to open the destination app via its package id and to pass the params via intent.

window.plugins.launcher.canLaunch({ packageName },() => {  window.plugins.launcher.launch(
{ packageName: packageName, extras: intentParams },
() => { console.log(`Switched to ${appName} app`) },
() => {
UICommunication.showErrorAlert(`An error occurred while launching ${appName} app. Please try again.`);}
);
},
() => {
// app not found.
UICommunication.showErrorAlert(`${appName} app is not installed on device.`);
}
);

Now as you can see in the example above , I am passing some intent params here. These are the params I want to pass to the KOTLIN app as the extras.

You can create intent params as shown below.

let intentParams = [];
intentParams.push({
name: "deliveryNumber", //key for this value
value: '123456, //actual value
dataType: "String", //data type
});
intentParams.push({
name: "doorNumber", //key for this value
value: '111', //actual value
dataType: "String", //data type
});

now you have the ability to create multiple types on params to be passed via intent. below is the list that the plugin supports.

extras: [
{"name":"myByte", "value":1, "dataType":"Byte"},
{"name":"myByteArray", "value":[1,0,2,3], "dataType":"ByteArray"},
{"name":"myShort", "value":5, "dataType":"Short"},
{"name":"myShortArray", "value":[1,2,3,4], "dataType":"ShortArray"},
{"name":"myInt", "value":2000, "dataType":"Int"},
{"name":"myIntArray", "value":[12,34,56], "dataType":"IntArray"},
{"name":"myIntArrayList", "value":[123,456,789], "dataType":"IntArrayList"},
{"name":"myLong", "value":123456789101112, "dataType":"Long"},
{"name":"myLongArray", "value":[123456789101112,121110987654321], "dataType":"LongArray"},
{"name":"myFloat", "value":12.34, "dataType":"Float"},
{"name":"myFloatArray", "value":[12.34,56.78], "dataType":"FloatArray"},
{"name":"myDouble", "value":12.3456789, "dataType":"Double"},
{"name":"myDoubleArray", "value":[12.3456789, 98.7654321], "dataType":"DoubleArray"},
{"name":"myBoolean", "value":false, "dataType":"Boolean"},
{"name":"myBooleanArray", "value":[true,false,true], "dataType":"BooleanArray"},
{"name":"myString", "value":"this is a test", "dataType":"String"},
{"name":"myStringArray", "value":["this","is", "a", "test"], "dataType":"StringArray"},
{"name":"myStringArrayList", "value":["this","is","a","test"], "dataType":"StringArrayList"},
{"name":"myChar", "value":"T", "dataType":"Char"},
{"name":"myCharArray", "value":"this is a test", "dataType":"CharArray"},
{"name":"myCharSequence", "value":"this is a test", "dataType":"CharSequence"},
{"name":"myCharSequenceArray", "value":["this","is a", "test"], "dataType":"CharSequenceArray"},
{"name":"myCharSequenceArrayList", "value":["this","is a", "test"], "dataType":"CharSequenceArrayList"},
{"name":"myParcelable", "value":"http://foo", "dataType":"Parcelable", "paType":"Uri"},
{"name":"myParcelableArray", "value":["http://foo","http://bar"], "dataType":"ParcelableArray", "paType":"Uri"},
{"name":"myParcelableArrayList", "value":["http://foo","http://bar"], "dataType":"ParcelableArrayList", "paType":"Uri"},
{"name":"mySparseParcelableArray", "value":{"10":"http://foo", "-25":"http://bar"}, "dataType":"SparseParcelableArray", "paType":"Uri"},
]

Once you do this , you will be triggering the destination app ( KOTLIN app in my case) with the following params passed in intent. Now lets see how you can receive them in the KOTLIN App.

KOTLIN App Implementation — Destination

Now in the KOTLin app , you can go to your MainActivity.kt file.

Here you have access to the intent param in the onCreate() method as shown below.

val delNbr = intent.getStringExtra("deliveryNumber")
//OR
val delNbr = intent.getSerializableExtra("deliveryNumber")

You do need to know the exact keys to access them from intent else you can get the entire intent as a bundle and try to extract them out of it.

Once you have the intent value , you can either pass it down to the required component or you can save it and access it from wherever required.

Note — the plugin does not allow you to pass objects i.e. multiple values inside 1 key hence you need to be careful about how many values you pass via intent. Ideally it should be minimal.

Conclusion

Hopefully this helped you in understanding 1 way to pass params from a cordova app to KOTLIN app via intent params.

Do checkout my other articles and follow for more content on Javascript, React JS and KOTLIN

--

--

--

A new tech publication by Start it up (https://medium.com/swlh).

Recommended from Medium

React Routing and Getting You Started.

Introduction to Semantics tags in HTML5

AWS Lambda Function Lab

Component Will Unmount React Hook

Variables

What Effects? Easily add Data Services to NGRX with Dynamic NGRX

Node.js for Beginners: How to Get Started

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Sumeet Bhalla

Sumeet Bhalla

Javascript Professional experienced in creating Mobile/Web Apps using React/React Native. New to KOTLIN. Passionate about photography and solving problems.

More from Medium

Migration of ROOM DB using Auto Migrations

Say goodbye to Flakiness : Testing Coroutines and Kotlin Flows [Part 1]

Kotlin Coroutine

Follow these simple Kotlin rules to write unbreakable code and stay 99+% crash-free

How and When to convert a Utility function in Kotlin to an Extension Function