0

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>
Kalai Selvi
  • 157
  • 11
  • Using `Activity` for this is the wrong architectural approach. You can't perform these kind of asynchronous tasks with activities. You need a `Service` for this. – David Wasser Mar 18 '22 at 15:51
  • @DavidWasser, could you please share me the example or code snippet for that – Kalai Selvi Mar 19 '22 at 09:19
  • Sorry, no. You should just search for a tutorial about how to use Android `Service`. I don't know enough about what you want to do and I'm not going to write the code for you. – David Wasser Mar 19 '22 at 13:33

0 Answers0