I am on ActivityA, trying to start ActivityB with String input, make some work, and return String output back to ActivityA. I have created Activity Result API helper class, based on this tutorial and ActivityResultContract (both listed below). I regester for activity result in onCreate() method of ActivityA:

private lateinit var linkLauncher: ActivityResultHandler<String, String?>

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    linkLauncher = ActivityResultHandler(this, extWebLinkAction, GetExtWebLinkContract())
}

The problem is when I call linkLauncher.execute() method which in turn calles ActivityResultLauncher's launch() method, both contracts methods get called before ActivityB even launches.

And here is how I am trying to return result from ActivityB to ActiviteA:

setResult(Activity.RESULT_OK, Intent().apply {
        putExtra(LINK_TRANSPORT_EXTRA, link)
})

Listing of mentioned classes.

Helper class:

import androidx.activity.ComponentActivity
import androidx.activity.result.ActivityResultCallback
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.ActivityResultRegistry
import androidx.activity.result.contract.ActivityResultContract
import androidx.lifecycle.LifecycleOwner
import java.util.*

interface IActivityResult<I> {
    fun execute(input: I)
}

class ActivityResultHandler<I, O>(
    private val activity: ComponentActivity,
    private val func: (O) -> Unit,
    private val contract: ActivityResultContract<I, O>
) : IActivityResult<I> by ActivityResultHandlerImpl(
    activity.activityResultRegistry,
    activity,
    func,
    contract
) {
    private class ActivityResultHandlerImpl<I, O>(
        private val registry: ActivityResultRegistry,
        private val lifecycleOwner: LifecycleOwner,
        private val func: (O) -> Unit,
        private val contract: ActivityResultContract<I, O>
    ): IActivityResult<I> {

        private val callback =  ActivityResultCallback<O> { output ->
            func(output)
        }

        private var resultLauncher: ActivityResultLauncher<I>
            = registry.register(UUID.randomUUID().toString(), lifecycleOwner, contract, callback)

        override fun execute(input: I) {
            resultLauncher.launch(input)
        }

    }
}

And here is my ActivityResultContract:

import android.app.Activity
import android.content.Context
import android.content.Intent
import androidx.activity.result.contract.ActivityResultContract
import com.zultys.mobiledroid.mvvm.ui.chat.group.GroupActivity
import com.zultys.mobiledroid.mvvm.ui.profile.extweb.ExtWebLinkFragment
import com.zultys.mobiledroid.utils.extensions.logd

class GetLinkContract: ActivityResultContract<String, String?>() {
    override fun createIntent(context: Context, input: String): Intent =
        context.intentFor<ActivityB>
               .newTask()
               .putExtra(GROUP_ID_EXTRA, groupId)

    override fun parseResult(resultCode: Int, intent: Intent?): String? =
        when {
            resultCode != Activity.RESULT_OK -> null
            else -> intent?.getStringExtra(LINK_TRANSPORT_EXTRA)
        }
}
1

There are 1 best solutions below

0
On BEST ANSWER

The reason why parseResult() got called right after createIntent() was .newTask() flag in createIntent() method. Also, as ianhanniballake remarked key shouldn't be random.