Android中Kotlin实现FirebaseUI点击退出登录返回登录界面
How to Redirect to Sign-In Screen After FirebaseUI Sign-Out in Android (Kotlin)
Hey there! I see you're a Kotlin beginner working with FirebaseUI for Android, and you want to get users back to the sign-in interface once they tap the sign-out button. Let's adjust your code to make that work smoothly.
The Issue in Your Current Code
Right now, your signOut call only handles the authentication sign-out, but it doesn't trigger navigation back to the sign-in screen. We need to add logic inside the addOnCompleteListener to launch the sign-in intent once the sign-out is successful.
Modified Code with Fix
Here's your updated MainActivity with the sign-out redirection implemented:
class MainActivity : AppCompatActivity() { private val RC_SIGN_IN = 123 private val auth = FirebaseAuth.getInstance() // Choose authentication providers private val providers = arrayListOf( AuthUI.IdpConfig.EmailBuilder().build(), AuthUI.IdpConfig.PhoneBuilder().build(), AuthUI.IdpConfig.GoogleBuilder().build() ) override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) signout.setOnClickListener { v -> AuthUI.getInstance() .signOut(this) .addOnCompleteListener { task -> if (task.isSuccessful) { // Sign-out succeeded, launch the sign-in screen val signInIntent = AuthUI.getInstance() .createSignInIntentBuilder() .setAvailableProviders(providers) .build() startActivity(signInIntent) // Finish the current activity so users can't navigate back to MainActivity via back button finish() } else { // Handle sign-out failure (e.g., show error message) Toast.makeText(this, "Failed to sign out", Toast.LENGTH_SHORT).show() } } } // Check if user is already signed in if (auth.currentUser == null) { // No user signed in, launch sign-in intent startActivityForResult( AuthUI.getInstance() .createSignInIntentBuilder() .setAvailableProviders(providers) .build(), RC_SIGN_IN ) } // If user is signed in, do nothing (stay on MainActivity) } override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { super.onActivityResult(requestCode, resultCode, data) if (requestCode == RC_SIGN_IN) { val response = IdpResponse.fromResultIntent(data) if (resultCode == Activity.RESULT_OK) { // User signed in successfully, you can add logic here (e.g., welcome message) val user = FirebaseAuth.getInstance().currentUser Toast.makeText(this, "Welcome back, ${user?.displayName}!", Toast.LENGTH_SHORT).show() } else { // Sign-in failed, handle the error (e.g., show message) Toast.makeText(this, "Sign-in failed", Toast.LENGTH_SHORT).show() } } } }
Key Changes Explained
- Sign-Out Navigation: Inside
addOnCompleteListener, we create and launch the FirebaseUI sign-in intent again once the sign-out is successful. We also callfinish()to closeMainActivity, so users can't press the back button to return to it without signing in again. - Error Handling: Added basic error handling for both sign-out and sign-in failures using
Toastmessages to give clear feedback to the user. - Cleaned Up User Check: Simplified the
currentUsercheck to directly launch the sign-in intent if no user is signed in, making the code more readable.
Additional Tips
- Double-check that your
signoutbutton is properly linked in youractivity_main.xmllayout (usingandroid:id="@+id/signout"). - You can customize the sign-in screen further (like adding a logo or terms of service link) using FirebaseUI's builder methods such as
setLogo()orsetTosUrl().
内容的提问来源于stack exchange,提问作者Khagendra
相关产品推荐
相关产品推荐

