简体   繁体   English

如何修复此AuthStateListener问题

[英]How do I fix this AuthStateListener issue

I have two activities: SignIn and SignUp . 我有两个活动: SignInSignUp Each of them has an AuthStateListener. 他们每个人都有一个AuthStateListener。

The problem is that AuthStateListener from SignIn activity gets called when app is in SignUp activity and authentication state is changed (Found this when I logged in both listener). 问题是,当应用程序处于SignUp活动并且身份验证状态发生更改时,将调用来自SignIn活动的AuthStateListener(当我同时登录两个侦听器时找到此内容)。

onCreate method of SignIn : signIn的onCreate方法:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sign_in);

    mAuth = FirebaseAuth.getInstance();

    mAuth.addAuthStateListener(new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
            progressBar.setVisibility(View.INVISIBLE);
            if (mAuth.getCurrentUser() != null && isEmailVerified()) {
                Toast.makeText(SignIn.this, "Signed In", Toast.LENGTH_SHORT).show();
                finish();
                startActivity(new Intent(SignIn.this, UserProfile.class));
            } else if (mAuth.getCurrentUser() != null) {
                mAuth.getCurrentUser().sendEmailVerification().addOnCompleteListener(new OnCompleteListener<Void>() {
                    @Override
                    public void onComplete(@NonNull Task<Void> task) {
                        Toast.makeText(SignIn.this, "Verification email sent You can sign in once your account is verified.", Toast.LENGTH_SHORT).show();
                        mAuth.signOut();
                    }
                });
            }
        }
    });

   ........
}

onCreate method of SignUp : SignUp的onCreate方法:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sign_up);

    mAuth = FirebaseAuth.getInstance();

    .....

    mAuth.addAuthStateListener(new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
            if (mAuth.getCurrentUser() != null) {
                verifyEmail();
            }
        }
    });
}

What can be done to fix this? 可以做些什么来解决这个问题?

If you don't want an AuthStateListener to be called anymore, you need to unregister it by calling removeAuthStateListener . 如果你不希望一个不AuthStateListener再被调用,你需要通过调用注销它removeAuthStateListener

This means you need to keep track of the listener, so: 这意味着您需要跟踪侦听器,因此:

listener = new FirebaseAuth.AuthStateListener() {
  ...
}
mAuth.addAuthStateListener(listener);

You'd typically do that in the opposite lifecycle event of where you register them. 您通常会在注册它们的相反生命周期事件中执行此操作。 In your case I'd recommend moving the addAuthStateListener on onStart and then unregister it in onStop or in onPause with 在您的情况下,我建议在onStart上移动addAuthStateListener ,然后在onStoponPause取消注册它

mAuth.removeAuthStateListener(listener)

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

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