Java .getCurrentUser()从不返回null-firebase

Java .getCurrentUser()从不返回null-firebase,java,firebase,firebase-authentication,Java,Firebase,Firebase Authentication,我正在使用facebook为我的应用程序创建身份验证,一切正常,只是当我注销facebook时,它仍然会将我传递到应用程序的主菜单。我假设这意味着即使我已注销,.getCurrentUser()也不会返回null 我试过注释updateUI();在我下面的代码中,这似乎解决了问题,但是我希望此代码能够正常工作 FirebaseUser currentUser = mAuth.getCurrentUser(); if(currentUser != null) { u

我正在使用facebook为我的应用程序创建身份验证,一切正常,只是当我注销facebook时,它仍然会将我传递到应用程序的主菜单。我假设这意味着即使我已注销,.getCurrentUser()也不会返回null

我试过注释updateUI();在我下面的代码中,这似乎解决了问题,但是我希望此代码能够正常工作

    FirebaseUser currentUser = mAuth.getCurrentUser();

    if(currentUser != null) {
       updateUI();
    }

您需要附加一个authstate侦听器

“在某些情况下,getCurrentUser将返回一个非空的FirebaseUser,但基础令牌无效。例如,如果用户在另一个设备上被删除,而本地令牌尚未刷新,则可能会发生这种情况。在这种情况下,您可能会获得一个有效的用户getCurrentUser,但随后对经过身份验证的资源的调用将失败

getCurrentUser也可能返回null,因为auth对象尚未完成初始化

如果附加AuthStateListener,则每次基础令牌状态更改时都会收到回调。这对于响应上述边缘情况非常有用。”


尝试使用AuthStateListener,示例:

//Declaration and defination
private FirebaseAuth firebaseAuth;
FirebaseAuth.AuthStateListener authStateListener = new FirebaseAuth.AuthStateListener() {
    @Override
    public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
        if (firebaseAuth.getCurrentUser() != null){
            //Do anything here which needs to be done after user is set is complete
            updateUI();
        }
        else {
        }
    }
};

//Init and attach
firebaseAuth = FirebaseAuth.getInstance();
firebaseAuth.addAuthStateListener(authStateListener);

你的updateUI()是做什么的?私有的void updateUI(){Toast.makeText(MainActivity.this,“你已登录”,Toast.LENGTH_LONG.show();Intent Intent=new Intent(MainActivity.this,Menu.class);startActivity(Intent);finish();}那么,当你注释掉
updateUI()
时,问题解决了吗?您应该查看该方法并检查代码。如果使用authStateListener,我是否应该删除以前使用的方法?请共享以前的方法,然后我才能对其进行评论。代码太多,所以我创建了这个collabedit.com文件。您没有将侦听器附加到mAuth,我已在collabedit mAuth.addAuthStateListener(mAuthStateListener)中对其进行了编辑;我附加了onStart方法。但是如果我添加了mAuth.addAuthStateListener(mAuthStateListener);在onCreate方法的末尾,它不起作用。打开应用程序时,我仍然会被重定向到菜单活动。