我试图实现一个GoogleSignIn与jetpack撰写。我能够启动GoogleSignIn,但我没有得到结果,它不会导航到下一个屏幕。我该怎么做呢?
这是我正在尝试的代码:
const val RC_SIGN_IN = 123
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build()
val mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
val result = remember { mutableStateOf<Client?>(null)}
val launcher = rememberLauncherForActivityResult(
ActivityResultContracts.StartActivityForResult()){
if (it.resultCode == RC_SIGN_IN) {
val task = GoogleSignIn.getSignedInAccountFromIntent(it.data)
try {
val account: GoogleSignInAccount? =
task.getResult(ApiException::class.java)
val client = Client(
account?.id!!,
account.displayName!!,
account.photoUrl!!.toString(),
account.email!!
)
Log.d("TAG", client.toString())
viewModel.addClient(client)
result.value = client
navController.navigate(Screen.HomeScreen.route)
} catch (e: ApiException) {
Toast.makeText(this, "login failed", Toast.LENGTH_LONG).show()
}
}
}
Button(onClick = {launcher.launch(mGoogleSignInClient.signInIntent)})
你可以这样做
@Preview
@Composable
fun GoogleSignInButton(){
Column {
val signInBtn = SignInButton(LocalContext.current)
AndroidView(factory = {signInBtn})
}
}