I am having issues with `registerForActivityResult...
# compose
j
I am having issues with
registerForActivityResult
. I see the result when I override it in Activity, but the callback of the register.... function is not executing. The Intent in question is a GoogleSignInIntent.
Copy code
@Composable
fun Screen() {
    val signInLauncher =
        registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
            viewModel.processActivityResult(it, ctx as Activity)
        }
    
    AndroidView(factory = {
        return@AndroidView SignInButton(it).apply {
            setOnClickListener {
                signInLauncher.launch(viewModel.signInIntent)
            }
            setColorScheme(colorScheme)
        }
    }, Modifier.padding(16.dp))
}
I copy pasted the sample in the same place and it works fine, however my own callback is never executed (https://developer.android.com/reference/kotlin/androidx/activity/compose/package-summary#registerforactivityresult)
z
You need to remember the launcher otherwise it won't work
i
You should not need to remember the launcher. Does the same code work with one of the prebuilt contracts?
n
Thanks for this tip… I wrote an article about it (in Portuguese) 😄 https://nglauber.medium.com/startactivityforresult-e-requestpermissions-no-jetpack-compose-6e7754346802
j
it is a prebuilt contract? And yes, the sample ImagePreview works
i
If some other contract works just fine, then the problem isn't with your Compose code (the type of contract and Intent is totally opaque at all of the API levels - it is just passed along).
FWIW, the
GoogleSignIn
APIs for requestPermissions that explicitly take an Activity or Fragment are not at all set up to work in Compose
j
Yeah, but I really have absolutely no idea why registerForActivityResult would not work with Google SignIn. I'm trying to setup a new Firebase project and reproduce it in an empty project True, but I thought just launching the Intent would work, apparently I was wrong
i
Does the launching not work, or does the receiving the result not work?
j
receiving the result
I thought the receiver might be disposed, but other Contracts work fine, so can't be that. I also debugged the Activity (since it does show up when overriding the result method), and I believe the registry for it contained the listener, so it should've been executed
i
I'd make sure you're using Activity Compose 1.3.0-alpha04 as there were some fixes in that release. I'd be very interested if you're able to reproduce it in a project that uses alpha04
j
I do
Got back to it, it works in the new Sample App In the real app: For some reason,
registerForActivityResult
is executed twice (initialized -> onDispose -> initialize). I logged this: • Registering with a key (the UUID key always stays the same) -> gets assigned request code 1666007090 • Deleting key from registry • Adding key again -> gets assigned request code 1573315457 The intent request Code that gets launched is however 1666007090, which does not work
i
That sounds like precisely the issue that alpha04 fixed - if you have a pending launch and recompose (i.e., go through the unregister/re-register flow you describe), you'll get the same request code back when giving the same key
So...really double and triple check you're using alpha04 😛
j
The sources that android studio shows are correct too
I'll just give up at this point, casting it to an Activity and calling the
registerForActivityResult
directly
i
I don't think that's safe to do, to be honest (make sure you're using LeakCanary to make sure you aren't leaking your callback / captured variables). You can certainly use the Gradle
dependencies
task to actually see what the resolved version is
j
./gradlew app:dependencies help | grep "activity"
does only show 1.3.0-alpha04 being resolved