Firebase Android onAuthStateChanged called twice

前端 未结 6 1123
忘了有多久
忘了有多久 2020-12-02 20:16

I\'ve start working with new Firebase SDK.

When I\'m doing user login, I\'m onAuthStateChanged method is being called twice with same state (etc. user sign in).

6条回答
  •  青春惊慌失措
    2020-12-02 20:31

    My workaround is to use a Boolean declared globally to flag if onAuthStateChanged has need called before.

    private Boolean authFlag = false;
     mAuthListener = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull final FirebaseAuth firebaseAuth) {
                if (firebaseAuth.getCurrentUser() != null) {
                    if(authFlag== false) {
                        // Task to perform once
                        authFlag=true;
                    }
                }
            }
        };
    

提交回复
热议问题