如何使用 Jetpack Compose 处理 Activity.onActivityResult()? [英] How to handle Activity.onActivityResult() with Jetpack Compose?

查看:235
本文介绍了如何使用 Jetpack Compose 处理 Activity.onActivityResult()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实施登录提示在我的 Android 应用程序中使用 Jetpack Compose,但此 API 需要 Activity 才能工作.

fun showPhoneNumberHint(activity: Activity) {val 提示请求:HintRequest = HintRequest.Builder().setPhoneNumberIdentifierSupported(true).建造()val 意图 = Auth.CredentialsApi.getHintPickerIntent(apiClient,hintRequest)val 请求代码 = 12345尝试 {startIntentSenderForResult(activity, intent.intentSender, requestCode, null, 0, 0, 0, null)} catch(异常:SendIntentException){//错误处理}}

所以我想我必须将 Activity 对象一直传递到需要它的 Composable,这看起来不是很干净,但它应该可以工作.

但是现在提示的结果将在 Activity 的 onActivityResult() 中接收,我不确定将它返回到需要它的 Composable 的正确方法是什么.>

是否有一些干净/标准/替代的方法来做到这一点?最好我只是将所有这些逻辑都包含在 Composable 中.

解决方案

我最终使用 rememberLauncherForActivityResult 结合 ActivityResultContracts.StartIntentSenderForResult() 契约来监听结果.这将返回一个可用于启动意图的 launcher.

我现在使用的是 Credentials.getClient,而不是需要已弃用的 GoogleApiClientAuth.CredentialsApi.为此,我仍然需要使用 LocalContext.current 获得的 Activity.

val phoneNumberHintLauncher = rememberLauncherForActivityResult(合同 = ActivityResultContracts.StartIntentSenderForResult()){如果 (it.resultCode != RESULT_OK) {return@rememberLauncherForActivityResult}val 凭证:凭证?= it.data?.getParcelableExtra(Credential.EXTRA_KEY)val 提示结果 = 凭据?.id如果(提示结果!== 空){电话号码 = 提示结果}}val 上下文 = LocalContext.currentLaunchedEffect(Unit) {val 提示请求:HintRequest = HintRequest.Builder().setPhoneNumberIdentifierSupported(true).建造()val phoneNumberHintIntent = Credentials.getClient(context).getHintPickerIntent(hintRequest)phoneNumberHintLauncher.launch(IntentSenderRequest.Builder(phoneNumberHintIntent).建造())}

I am trying to implement sign-in hints in my Android app using Jetpack Compose, but this API requires an Activity to work.

fun showPhoneNumberHint(activity: Activity) {
    val hintRequest: HintRequest = HintRequest.Builder()
        .setPhoneNumberIdentifierSupported(true)
        .build()

    val intent = Auth.CredentialsApi.getHintPickerIntent(apiClient, hintRequest)
    val requestCode = 12345

    try {
        startIntentSenderForResult(activity, intent.intentSender, requestCode, null, 0, 0, 0, null)
    } catch (exception: SendIntentException) {
        // Error handling
    }
}

So I guess that I'll have to pass the Activity object all the way down to the Composable where it's needed, which doesn't seem very clean but it should work.

But now the result of the hint will be received in the Activity's onActivityResult() and I'm not sure what the right way is to get it back to the Composable where it's needed.

Is there some clean/standard/alternative way to do this? Preferably I'd just keep all of this logic contained inside the Composable.

解决方案

I ended up using rememberLauncherForActivityResult in combination with the ActivityResultContracts.StartIntentSenderForResult() contract to listen for the result. This returns a launcher that can be used to start the intent.

Instead of Auth.CredentialsApi, which requires the deprecated GoogleApiClient, I'm now using the Credentials.getClient. For this I still needed an Activity which I got using LocalContext.current.

val phoneNumberHintLauncher = rememberLauncherForActivityResult(
    contract = ActivityResultContracts.StartIntentSenderForResult()
) {
    if (it.resultCode != RESULT_OK) {
        return@rememberLauncherForActivityResult
    }

    val credential: Credential? = it.data?.getParcelableExtra(Credential.EXTRA_KEY)
    val hintResult = credential?.id

    if (hintResult !== null) {
        phoneNumber = hintResult
    }
}

val context = LocalContext.current

LaunchedEffect(Unit) {
    val hintRequest: HintRequest = HintRequest.Builder()
        .setPhoneNumberIdentifierSupported(true)
        .build()

    val phoneNumberHintIntent = Credentials.getClient(context)
        .getHintPickerIntent(hintRequest)

    phoneNumberHintLauncher.launch(
        IntentSenderRequest.Builder(phoneNumberHintIntent)
            .build()
    )
}

这篇关于如何使用 Jetpack Compose 处理 Activity.onActivityResult()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆