Firebase AuthListener多次调用

时间:2018-10-01 14:25:44

标签: java android firebase firebase-authentication

我已经在SO中对此问题进行了研究,发现有2或3个问题,但仍不是解决该问题的正式答案,问题是我的AuthListener在登录我的应用后一直在监听更改,并且也会触发很多次。看看

我想要做的是用户打开应用程序并进入登录活动,它将监听用户是否已经登录以便将他带到第一个活动。现在,要做到这一点,我可以在演示者中完成

 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");
                }
                // ...
            }
        };

    }

然后在我看来

onCreate()

presenterPrincipal.checkUserLogin();

我在这里附上听众

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

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

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

但是当我登录我的应用程序时,我得到了

  

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

并且继续前进,我的UI冻结。

有关如何解决此问题的任何提示?

1 个答案:

答案 0 :(得分:0)

您要删除LoginActivity的onStop()中的 authStateListener 。为了解决此问题,用户登录后应删除authStateListener。

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
            }
            // ...
        }
    };

}

使用共享首选项存储用户是否已登录,而不是每次用户启动应用程序时都添加authStateListener。