'How to call resultLauncher that exists in the activity from a composable function?
I'm trying to implement One-Tap together with Jetpack Compose. The startDestination in my NavGraph is a screen called AuthScreen. Inside the ativity I have defined a launcher like this:
class MainActivity : AppCompatActivity() {
private lateinit var resultLauncher: ActivityResultLauncher<IntentSenderRequest>
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
resultLauncher = registerForActivityResult(ActivityResultContracts.StartIntentSenderForResult()) {
if (result.resultCode == RESULT_OK) {
//Do stuf
}
}
}
}
And inside the AuthScreen I try to sign-in like this:
fun AuthScreen(
viewModel: AuthViewModel = hiltViewModel()
) {
Scaffold(
topBar = {
DarkTopBar()
}
) {
Box(
modifier = Modifier.fillMaxSize().padding(bottom = 48.dp)
) {
Button(
onClick = {
viewModel.oneTapSignIn()
}
) {
Text(
text = "Sign-in"
)
}
}
}
when(val response = viewModel.oneTapSignInState.value) {
is Response.Success -> {
val beginSignInResult = response.data
val intent = IntentSenderRequest.Builder(beginSignInResult.pendingIntent.intentSender).build()
//resultLauncher.launch(intent) 👈 Here is the problem.
}
is Response.Error -> print(response.error)
}
}
But I cannot use resultLauncher since it's placed in the activity and not inside the composable function. Both exist in different files. How to call resultLauncher that exists in the activity from a composable function?
Or is there a better way? For example, to move the resultLauncher inside the composable function? Any ideas will highly appreciate it. Thanks
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
