Notbad
Notbad

Reputation: 6306

FirebaseAuth onAuthStateChanged always fired up on app startup

I would like to get notified when the user state in my app changes. I mean, when it just gets logged in and when it just gets logged out. I don't want to be notified when user token is refreshed or everytime the app fires up and user is autologged in.

I'm using the expected handler for this:

FirebaseAuth.getInstance().addAuthStateListener(new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                FirebaseUser user = firebaseAuth.getCurrentUser();
                if (user == null) {
                    _restUserDataAndUI();
                } else
                    _refreshUserDataAndUI();
            }
        });

The problem is that it fires up every time my app is fired up and when token for the user changes.

Is there any way to avoid this?

Upvotes: 0

Views: 257

Answers (1)

Alex Mamo
Alex Mamo

Reputation: 1

FirebaseAuth's addAuthStateListener () method:

Registers a listener to changes in the user authentication state.

The listeners call back in the UI thread, on the following events:

  • Right after the listener has been registered
  • When a user signs in
  • When the current user signs out
  • When the current user changes

The single way in which you can change this behaviour is to unregister a listener using removeAuthStateListener(AuthStateListener).

Upvotes: 1

Related Questions