繁体   English   中英

在jetpack compose中没有从rememberLauncherForActivityResult()获得结果

[英]Not Getting result from rememberLauncherForActivityResult() in jetpack compose

我正在调用StartIntentSenderForResult()但它没有被调用。

    val authResult = rememberLauncherForActivityResult(
        contract = ActivityResultContracts.StartIntentSenderForResult()
    ) {
        Log.d("appDebug", "called!!!") // not get called
    }

    oneTapClient.beginSignIn(signUpRequest)
        .addOnSuccessListener(activity) { result ->
            try {
                // Calling here for result
                authResult.launch(
                    IntentSenderRequest
                        .Builder(result.pendingIntent.intentSender)
                        .build()
                )
            } catch (e: IntentSender.SendIntentException) {
                Log.d("appDebug", "CATCH : ${e.localizedMessage}")
            }
        }
        .addOnFailureListener(activity) { e ->
            Log.d("appDebug", "FAILED : ${e.localizedMessage}")
        }

如果有人有同样的问题,那么只需使用这个可组合而不是rememberLauncherForActivityResult()

感谢@Róbert Nagy 参考: https : //stackoverflow.com/a/65323208/15301088

我从原始帖子中删除了一些不推荐使用的代码,现在它对我来说很好用。

@Composable
fun <I, O> registerForActivityResult(
    contract: ActivityResultContract<I, O>,
    onResult: (O) -> Unit
): ActivityResultLauncher<I> {
    val owner = LocalContext.current as ActivityResultRegistryOwner
    val activityResultRegistry = owner.activityResultRegistry

    // Tracking current onResult listener
    val currentOnResult = rememberUpdatedState(onResult)

    // Only need to be unique and consistent across configuration changes.
    val key = remember { UUID.randomUUID().toString() }

    DisposableEffect(activityResultRegistry, key, contract) {
       onDispose {
           realLauncher.unregister()
       }
   }

   return realLauncher
}

例如

val registerActivityResult = registerForActivityResult(
    contract = ActivityResultContracts.StartIntentSenderForResult()
) {
    // handle your response
}

// just call launch and pass the contract
registerActivityResult.launch(/*Your Contract*/)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM