简体   繁体   中英

How to return from Activity to Fragment to notify him about the exit from the account

I have 2 Activities. On the first one I have a fragmentContainerView in which I use multiple Fragments(Splash, Sign In, Create Account). On the second Activity I have a menu drawer. In this menu drawer, I have a TextView, upon clicking which the user should log out of the account and return to Splash. Splash has a nextBTN button. If the user is not authorized, then he will be sent to Sign In or Create Account. If the user is already authorized, then he will be sent to 2 Activity. To log into an account or create an account, I use the Firebase service.

Problem: when a user presses the Sign Out button, he is sent to Splash, but first Activity does not know that he is logged out. And there is a check

auth.currentUser != null 

and every time when nextBTN is clicked, it sends the user to 2 Activities anyway.

I decided to check and added another button on Splash with an account exit code, and indeed, if you first use the account exit button on Splash, and then nextBTN, then in this case it suggests logging into your account or creating an account.

How can I tell Splash(Fragment) from Activiti that the user has logged out of the account?

Code TextView (Sign Out) with 2 Activities:

fun onClickSignOut (view: View){
    auth.signOut()
    finish()
}

Button code to check if the user is currently logged in (Fragment Splash):

binding.nextBTN.setOnClickListener {
    if (auth.currentUser != null){
        navController.navigate(R.id.action_fragment_Splash_to_mainMenuActivity)
    } else {
        navController.navigate(R.id.action_fragment_Splash_to_fragment_Splash2)
    }
}

PS on Splash2 I have 2 buttons to redirect to Sign In or Create Account.

Account creation code (Fragment):

private fun init(view: View){
    navControl = Navigation.findNavController(view)
    auth = FirebaseAuth.getInstance()
}

private fun registerEvents(){

    binding.createAccBTN.setOnClickListener {

        val email = binding.emailET.text.toString().trim()
        val pass = binding.passwordET.text.toString().trim()
        val retypePass = binding.retypePasswordET.text.toString().trim()

        if (email.isNotEmpty() && pass.isNotEmpty() && retypePass.isNotEmpty()){
            if (pass == retypePass){
                auth.createUserWithEmailAndPassword(email, pass).addOnCompleteListener(
                    OnCompleteListener {
                        if (it.isSuccessful){
                            Toast.makeText(context , "Registered Successfully" , Toast.LENGTH_SHORT).show()
                            navControl.navigate(R.id.action_fragment_CreateAcc_to_mainMenuActivity)
                        } else {
                            Toast.makeText(context , it.exception?.message , Toast.LENGTH_SHORT).show()
                        }
                    })
            }
        }

    }
}

Sign In code (Fragment):

binding.signInBTN.setOnClickListener {

    val email = binding.emailET.text.toString().trim()
    val pass = binding.passwordET.text.toString().trim()

    if (email.isNotEmpty() && pass.isNotEmpty()){

            auth.signInWithEmailAndPassword(email, pass).addOnCompleteListener(
                OnCompleteListener {
                    if (it.isSuccessful){
                        Toast.makeText(context , "Login Successfully" , Toast.LENGTH_SHORT).show()
                        navControl.navigate(R.id.action_fragment_SignIn_to_mainMenuActivity)

                    } else {
                        Toast.makeText(context , it.exception?.message , Toast.LENGTH_SHORT).show()
                    }
                })

    }

}

I would be very grateful for any help.

Thanks everyone for your suggestions. But I solved it by changing one line.

Was auth.signOut() , became FirebaseAuth.getInstance().signOut()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM