I have an app that accepts two intent requests (external intent request) with coroutine. For the main activity, I have written the code as below.
class MainActivity : Activity(), myClassInterface {
lateinit var activityContext: ActivityContext
var exceptionError: java.lang.Exception? = null
val job = SupervisorJob()
val coroutineScope = CoroutineScope(Dispatchers.Default + job)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
try {
setContentView(R.layout.activity_main)
coroutineScope.launch {
val workflowController = myClassActivityController()
workflowController.handleRequest(activityContext)
}
}
catch (e: Exception) {
exceptionError = e
}
if (exceptionError != null) {
val errIntent = Intent(applicationContext, ErrorActivity::class.java)
errIntent.putExtra("exceptionMessage", exceptionError.toString())
startActivity(errIntent)
finish()
}
}
override fun setActivityResult(activityContext: ActivityContextInterface): Int {
setResult(RESULT_OK, activityContext.getIntentResponse())
return 0
}
override fun finishActivity(): Int {
coroutineScope.coroutineContext.cancelChildren()
finish()
return 0
}
}
In my manifest file, I have exposed my intent in order to accept the request.
When two requests are raised from another app, the coroutine handles the request and works in a preemptive manner.
It gives the result when both the request-response is obtained. For instance, if the first request is in progress, at the time the second request is raised and its performed and result is obtained but the second request- response is not transferred until the first request is obtained result.
So the manifest file looks like below,
<activity
android:name=".MainActivity"
android:permission=""
android:label="@string/app_name"
android:theme="@style/Theme.Transparent">
<intent-filter>
<action android:name="android.intent.action.GET_CONTENT" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="@string/route" />
<data
android:host="localhost"
android:port="8080" />
<data android:path="@string/task1" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.GET_CONTENT" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="@string/route" />
<data
android:host="localhost"
android:port="8080" />
<data android:path="@string/task2" />
</intent-filter>
</activity>