When Intent is received in Xamarin, ensure that only one instance is running at any given time

Viewed 459

The Xamarin App sends and receives requests to the "central microservice" every 5 seconds using a timer. We can do an "action" in two ways: using intent from another app or scanning a QR code from a web app. Our app sends a request to the "central microservice" when scanning a QR Code or receiving an Intent, and receives a request from the "central microservice" after "5" seconds.

When scanning a QR code, the "central microservice" sends a single request per call. However, while using Intent from another app, you may receive an additional request. Apps are executing in two instances on Intent.

In the AndroidManifest.xml file, I tried adding android:launchMode="singleInstance" and android:launchMode="singleTask".

In MainActivity.cs, I inserted LaunchMode = LaunchMode.SingleTask and LaunchMode = LaunchMode.SingleInstance.

In Another App (the Intent comes from), I inserted intent.addFlags(Intent.FLAG ACTIVITY REORDER TO FRONT).

Both Apps are still active (can be seen by minimizing the app). How can I ensure that only one instance of the app is running at any given time when the Intent is being received?

AndroidManifest.xml (Of Xamarin App, where the Intent will be sent to)

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:versionCode="10" android:versionName="1.0.10" package="com.example.XamarinApp" android:installLocation="internalOnly">
  <uses-sdk android:minSdkVersion="26" android:targetSdkVersion="30" />
  <application android:label="DemoApp" android:icon="@drawable/icon_white" android:allowBackup="false" android:fullBackupOnly="false" android:launchMode="singleInstance"></application>
  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  <uses-permission android:name="android.permission.CAMERA" />
  <uses-permission android:name="android.permission.USE_FINGERPRINT" />
  <uses-permission android:name="android.permission.WAKE_LOCK" />
  <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" tools:node="remove" />
  <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" tools:node="remove" />
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" tools:node="remove" />
</manifest>

Android App (From where the Intent is being sent to Xamarin App)

var appPackageName = "com.example.XamarinApp"
var appActivityName = "com.XamarinApp.activity"

val intent = Intent()
intent.action = appPackageName
intent.setClassName(appPackageName, appActivityName)
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT)

var firstName = "Hello"
Var lastName = "World"

intent.putExtra("firstName", firstName)
intent.putExtra("lastName", lastName)

AndroidSDK().signup(
                this@MainActivity,
                firstName,
                lastName)

Android SDK

fun signup(
    activity: Activity,
    first_Name: String,
    last_Name: String
) {

if (checkXamarinApp(activity, appPackageName)) {
val intent = Intent()
intent.action = appPackageName
intent.setClassName(appPackageName, appActivityName)
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT)

val firstName = first_Name
val lastName = last_Name

intent.putExtra("firstName", firstName)
intent.putExtra("lastName", lastName)
} else {
    redirectToPlayStore(activity)
}

Intent Filters

<intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />
    <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

Diagram enter image description here

1 Answers

There are four modes for the launchMode. According to the description, if you wanna single instance, you could use singleTask and singleInstance.

When you do this across applications, you could use singleInstance. It makes that the system doesn't launch any other activities into the task holding the instance. The activity is always the single and only member of its task.

For more details, you could refer to the link below. https://developer.android.com/guide/topics/manifest/activity-element#lmode

Related