简体   繁体   English

如何从Activity返回到Fragment通知他退出账号

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

I have 2 Activities.我有 2 个活动。 On the first one I have a fragmentContainerView in which I use multiple Fragments(Splash, Sign In, Create Account).在第一个上,我有一个 fragmentContainerView,我在其中使用了多个片段(启动、登录、创建帐户)。 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.在这个菜单抽屉中,我有一个 TextView,点击它用户应该注销帐户并返回到 Splash。 Splash has a nextBTN button. Splash 有一个 nextBTN 按钮。 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.如果用户已经被授权,那么他将被发送到 2 Activity。 To log into an account or create an account, I use the Firebase service.要登录帐户或创建帐户,我使用 Firebase 服务。

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.问题:当用户按下注销按钮时,他被发送到 Splash,但首先 Activity 不知道他已注销。 And there is a check还有一张支票

auth.currentUser != null 

and every time when nextBTN is clicked, it sends the user to 2 Activities anyway.每次单击 nextBTN 时,它都会将用户发送到 2 个活动。

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.我决定检查并在 Splash 上添加另一个带有帐户退出代码的按钮,事实上,如果您首先使用 Splash 上的帐户退出按钮,然后使用 nextBTN,那么在这种情况下,它会建议登录您的帐户或创建一个帐户。

How can I tell Splash(Fragment) from Activiti that the user has logged out of the account?我如何从 Activiti 中告诉 Splash(Fragment) 用户已经退出账户?

Code TextView (Sign Out) with 2 Activities:代码 TextView(注销)有 2 个活动:

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

Button code to check if the user is currently logged in (Fragment Splash):用于检查用户当前是否登录的按钮代码(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. PS 在 Splash2 上我有 2 个按钮可以重定向到登录或创建帐户。

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()auth.signOut() ,成为FirebaseAuth.getInstance().signOut()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将数据从 Activity 传递到 Fragment KOTLIN? - How to pass data from Activity to Fragment KOTLIN? 如何从特定片段的活动中禁用 onDestroy 方法? - How to disable an onDestroy method from activity in a specific fragment? Firebase 数据检索并将其从片段传递到新活动不起作用 - Firebase data retrieve and passing it from fragment to new activity not working 如何通过点击通知打开片段? - How to open fragment from the click of the notification? 如何从 Firebase 检索其他活动的数据? - How to retrieve data from Firebase on other activity? 当条件满足时,如何从 apacheBeam 中的 doFn lyfecycle 退出? - How to exit from doFn lyfecycle in apacheBeam,when a condition meets? Firebase - 如何取消自定义提供商与帐户的链接 - Firebase - How to unlink a custom provider from a account 如何将数据从一个 AWS 账户的 RDS 移动到另一个账户 - How do I move data from RDS of one AWS account to another account 如何从片段中的 Firebase 实时数据库中检索数据? - How to Retrieve Data from Firebase Realtime Database in Fragment? 如何从 Lambda 返回 promise - How to return a promise from Lambda
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM