Ad

How To Get Back To Login Screen Once User Click On Signout Button In Kotlin?

- 1 answer

I am a beginner with kotlin and i want to know how we can take users back to sign in screen using firebase-ui when user clicks on sign out button. i am trying to implement this on android.

class MainActivity : AppCompatActivity() {
private val RC_SIGN_IN = 123;
val auth=FirebaseAuth.getInstance()
// Choose authentication providers
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 {
                // ...
            }
    }

    if(auth.currentUser!=null)
    {

    }
    else
    {
        // Create and launch sign-in intent
        startActivityForResult(
            AuthUI.getInstance()
                .createSignInIntentBuilder()
                .setAvailableProviders(providers)
                .build(),
            RC_SIGN_IN)
    }
}
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) {                
            val user = FirebaseAuth.getInstance().currentUser
        } else {

        }
    }
}

}

Ad

Answer

val intent = Intent(context, LoginActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK)
startActivity(intent)
finish()
Ad
source: stackoverflow.com
Ad