0

I have researched for this problem in SO, I found like 2 or 3 questions but still not an official answer to solve the problem, the problem is that my AuthListener keeps listening to changes after I login to my app and it triggers too much times. Take a look

What I want to do is where the user opens the app and goes to the login activity , it will listen if the user is already logged in order to take him to the first activity. Now , to do that I do this in my presenter

 public FirebaseAuth.AuthStateListener checkUserLogin(){

        return mAuthListener = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                FirebaseUser user = firebaseAuth.getCurrentUser();
                if (user != null) {
                    // User is signed in
                    Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
                    mContext.startActivity(new Intent(mContext,VistaPrincipal.class));
                    ((Activity)mContext).finish();

                } else {
                    // User is signed out
                    Log.d(TAG, "onAuthStateChanged:signed_out");
                }
                // ...
            }
        };

    }

Then in my view

onCreate()

presenterPrincipal.checkUserLogin();

Here I attach the listeners

@Override
    protected void onStart() {
        super.onStart();

        mAuth.addAuthStateListener(presenterPrincipal.checkUserLogin());
    }

    @Override
    protected void onStop() {
        super.onStop();
        mAuth.removeAuthStateListener(presenterPrincipal.checkUserLogin());
    }

But then when I login into my app I get this

2018-10-01 11:16:29.505 11516-11516/com.example.macbook.firebasemvp D/Constraints: onAuthStateChanged:signed_in:0RWj5Ah3K9bYEkPImo9eLhkdA1n2 2018-10-01 11:16:30.070 11516-11516/com.example.macbook.firebasemvp D/Constraints: onAuthStateChanged:signed_in:0RWj5Ah3K9bYEkPImo9eLhkdA1n2 2018-10-01 11:16:30.635 11516-11516/com.example.macbook.firebasemvp D/Constraints: onAuthStateChanged:signed_in:0RWj5Ah3K9bYEkPImo9eLhkdA1n2 2018-10-01 11:16:31.206 11516-11516/com.example.macbook.firebasemvp D/Constraints: onAuthStateChanged:signed_in:0RWj5Ah3K9bYEkPImo9eLhkdA1n2 2018-10-01 11:16:31.633 11516-11516/com.example.macbook.firebasemvp D/Constraints: onAuthStateChanged:signed_in:0RWj5Ah3K9bYEkPImo9eLhkdA1n2 2018-10-01 11:16:32.459 11516-11516/com.example.macbook.firebasemvp D/Constraints: onAuthStateChanged:signed_in:0RWj5Ah3K9bYEkPImo9eLhkdA1n2 2018-10-01 11:16:33.181 11516-11516/com.example.macbook.firebasemvp D/Constraints: onAuthStateChanged:signed_in:0RWj5Ah3K9bYEkPImo9eLhkdA1n2 2018-10-01 11:16:34.036 11516-11516/com.example.macbook.firebasemvp D/Constraints: onAuthStateChanged:signed_in:0RWj5Ah3K9bYEkPImo9eLhkdA1n2 2018-10-01 11:16:34.617 11516-11516/com.example.macbook.firebasemvp D/Constraints: onAuthStateChanged:signed_in:0RWj5Ah3K9bYEkPImo9eLhkdA1n2 2018-10-01 11:16:35.442 11516-11516/com.example.macbook.firebasemvp D/Constraints: onAuthStateChanged:signed_in:0RWj5Ah3K9bYEkPImo9eLhkdA1n2 2018-10-01 11:16:35.864 11516-11516/com.example.macbook.firebasemvp D/Constraints: onAuthStateChanged:signed_in:0RWj5Ah3K9bYEkPImo9eLhkdA1n2 2018-10-01 11:16:36.410 11516-11516/com.example.macbook.firebasemvp D/Constraints: onAuthStateChanged:signed_in:0RWj5Ah3K9bYEkPImo9eLhkdA1n2 2018-10-01 11:16:37.456 11516-11516/com.example.macbook.firebasemvp D/Constraints: onAuthStateChanged:signed_in:0RWj5Ah3K9bYEkPImo9eLhkdA1n2 2018-10-01 11:16:38.784 11516-11516/com.example.macbook.firebasemvp D/Constraints: onAuthStateChanged:signed_in:0RWj5Ah3K9bYEkPImo9eLhkdA1n2 2018-10-01 11:16:39.637 11516-11516/com.example.macbook.firebasemvp D/Constraints: onAuthStateChanged:signed_in:0RWj5Ah3K9bYEkPImo9eLhkdA1n2 2018-10-01 11:16:40.772 11516-11516/com.example.macbook.firebasemvp D/Constraints: onAuthStateChanged:signed_in:0RWj5Ah3K9bYEkPImo9eLhkdA1n2

And keeps on going and going and my UI freezes.

Any hint on how to solve this issue?

Todd
  • 179
  • 15

1 Answers1

0

You are removing the authStateListener in onStop() of your LoginActivity. In order to solve this issue, you should remove the authStateListener once the user is signedIn.

public FirebaseAuth.AuthStateListener checkUserLogin(){

    return mAuthListener = new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
            FirebaseUser user = firebaseAuth.getCurrentUser();
            if (user != null) {
                // User is signed in
                mAuth.removeAuthStateListener(this);//removing auth state listener
                mContext.startActivity(new Intent(mContext,VistaPrincipal.class));
                ((Activity)mContext).finish();

            } else {
                // User is signed out
            }
            // ...
        }
    };

}

Use shared preference to store whether user is logged in or not, instead of adding authStateListener everytime when the user launches your app.

Samuvel P
  • 11
  • 2