Search code examples
androidkotlinregisterforactivityresult

registerForActivityResult won't seem to import Kotlin


Im trying to use the function registerForActivityResult in my android project made in android studio. But when I write "registerForActivityResult" to use the function all that is showing is create function. I thought registerForActivityResult was a remade function in Android an I don't know what to do to make it work. If any of you kind souls could help me I would deeply appreciate it, I can always gather more information if needed!

Update Im currently using the guide provided in this video. I am using android.activity.1.3.1 and the code that isn't working is this: private val signInLauncher = registerForActivityResult ( FirebaseAuthUIActivityResultContract() ) { res -> this.signInResult(res) }


Solution

  • Try manually casting to ComponentActivity.

    I've ended up creating an extension for this function:

    fun <I, O> Activity.registerForActivityResult(
        contract: ActivityResultContract<I, O>,
        callback: ActivityResultCallback<O>
    ) = (this as ComponentActivity).registerForActivityResult(contract, callback)
    

    It seems there is an open bug for AndroidStudio: Bug: unable to find registerForActivityResult on large project despite having the needed dependencies

    For using this, in your Activity when you need to request multiple permissions for example you could do something like this:

    registerForActivityResult(
        ActivityResultContracts.RequestMultiplePermissions()
    ) { result ->
        // Do something with the result 
    }