Android Firebase验证返回按钮
我的android应用程序中的firebase身份验证没有后退按钮。如果onActivityResult生成取消结果,我希望能够导航回主活动 如何启用此功能?我正在使用预构建的身份验证Android Firebase验证返回按钮,android,firebase-authentication,firebaseui,Android,Firebase Authentication,Firebaseui,我的android应用程序中的firebase身份验证没有后退按钮。如果onActivityResult生成取消结果,我希望能够导航回主活动 如何启用此功能?我正在使用预构建的身份验证 final int RC_SIGN_IN = 0; List<AuthUI.IdpConfig> providers = Arrays.asList( new AuthUI.IdpConfig.EmailBuilder().build(),
final int RC_SIGN_IN = 0;
List<AuthUI.IdpConfig> providers = Arrays.asList(
new AuthUI.IdpConfig.EmailBuilder().build(),
new AuthUI.IdpConfig.PhoneBuilder().build(),
new AuthUI.IdpConfig.GoogleBuilder().build()
);
Intent intent = AuthUI.getInstance()
.createSignInIntentBuilder()
.setAvailableProviders(providers)
.setTheme(R.style.FirebaseUI)
.build();
startActivityForResult(intent, RC_SIGN_IN);
final int RC\u SIGN\u IN=0;
列表提供程序=Arrays.asList(
新建AuthUI.IdpConfig.EmailBuilder().build(),
新建AuthUI.IdpConfig.PhoneBuilder().build(),
新建AuthUI.IdpConfig.GoogleBuilder().build()
);
Intent=AuthUI.getInstance()
.CreateSignInEntBuilder()
.setAvailableProviders(提供程序)
.setTheme(R.style.FirebaseUI)
.build();
startActivityForResult(意向、RC登录);
调用super.onBackPressed()
将导航回上一个活动
private static final int RC_SIGN_IN = 1;
private void signIn () {
Intent signInIntent = mGoogleSignInClient.getSignInIntent ();
startActivityForResult ( signInIntent, RC_SIGN_IN );
//Show user that authentication has started
}
@Override
public void onActivityResult ( int requestCode, int resultCode, Intent data ) {
super.onActivityResult ( requestCode, resultCode, data );
if (requestCode == RC_SIGN_IN) {
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent ( data );
try {
GoogleSignInAccount account = task.getResult ( ApiException.class );
assert account != null;
firebaseAuthWithGoogle ( account.getIdToken () );
} catch (ApiException e) {
Toast.makeText ( this, "Google Sign In failed", Toast.LENGTH_SHORT ).show ();
mGoogleSignInClient.signOut ();
mAuth.signOut ();
//Show user that authentication failed
//In your case navigate to MainActivity()
super.onBackPressed (); //Navigates to previous activity
}
}
}
private void firebaseAuthWithGoogle ( String idToken ) {
AuthCredential credential = GoogleAuthProvider.getCredential ( idToken, null );
mAuth.signInWithCredential ( credential )
.addOnCompleteListener ( this, task -> {
if (task.isSuccessful ()) {
FirebaseUser user = mAuth.getCurrentUser ();
if (user != null) {
//Authenticated successfully
} else {
//Try to authenticate again
//This might not happen in most scenarios
signIn ();
}
} else {
Toast.makeText ( this, "Google Sign In failed", Toast.LENGTH_SHORT ).show ();
mGoogleSignInClient.signOut ();
mAuth.signOut ();
//Show user that authentication failed
//In your case navigate to MainActivity()
super.onBackPressed (); //Navigates to previous activity
}
} );
}
我假设您使用的是firebase google身份验证。如果发生任何故障,以下代码片段可能会帮助您导航回MainActivity
要启动身份验证,请调用signIn()
方法
如果发生任何API异常,onActivityResult()
方法将导航回上一个活动
private static final int RC_SIGN_IN = 1;
private void signIn () {
Intent signInIntent = mGoogleSignInClient.getSignInIntent ();
startActivityForResult ( signInIntent, RC_SIGN_IN );
//Show user that authentication has started
}
@Override
public void onActivityResult ( int requestCode, int resultCode, Intent data ) {
super.onActivityResult ( requestCode, resultCode, data );
if (requestCode == RC_SIGN_IN) {
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent ( data );
try {
GoogleSignInAccount account = task.getResult ( ApiException.class );
assert account != null;
firebaseAuthWithGoogle ( account.getIdToken () );
} catch (ApiException e) {
Toast.makeText ( this, "Google Sign In failed", Toast.LENGTH_SHORT ).show ();
mGoogleSignInClient.signOut ();
mAuth.signOut ();
//Show user that authentication failed
//In your case navigate to MainActivity()
super.onBackPressed (); //Navigates to previous activity
}
}
}
private void firebaseAuthWithGoogle ( String idToken ) {
AuthCredential credential = GoogleAuthProvider.getCredential ( idToken, null );
mAuth.signInWithCredential ( credential )
.addOnCompleteListener ( this, task -> {
if (task.isSuccessful ()) {
FirebaseUser user = mAuth.getCurrentUser ();
if (user != null) {
//Authenticated successfully
} else {
//Try to authenticate again
//This might not happen in most scenarios
signIn ();
}
} else {
Toast.makeText ( this, "Google Sign In failed", Toast.LENGTH_SHORT ).show ();
mGoogleSignInClient.signOut ();
mAuth.signOut ();
//Show user that authentication failed
//In your case navigate to MainActivity()
super.onBackPressed (); //Navigates to previous activity
}
} );
}
我希望这个答案可以帮助你如果你问如何改变Firebase UI的工作方式,你可以通过复制其源代码并添加你想要的内容来做任何事情:调用
finish()
停止当前活动并返回不是更容易吗?嗨@DougStevenson!。是的,你说得对。调用finish()。正如@onStackOverflowListener的问题一样,如何导航回去,我只是使用了super.onBackPressed()
方法来实现这个目的。还有一个优点是,finish()
不能被覆盖。但是可以覆盖onBackPressed()
,这可能有助于在导航到上一个活动时进一步添加功能。我使用的是内置的身份验证系统。这是通过编写自己的身份验证流来实现的唯一方法。