注销按钮不起作用。 Android - 火力地堡

Sign out button doesn't works. Android - Firebase

注销无效。单击时没有任何反应。有人看错了吗?

代码太长 - 这是我的 MainActivity (https://www.paste.org/90991):

    public String userEmail, userName;
    private FirebaseAuth firebaseAuth;
    private FirebaseFirestore rootRef;
    private FirebaseAuth.AuthStateListener authStateListener;
    private GoogleApiClient googleApiClient;

    public String getUserEmail() {return userEmail;}

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);

        GoogleSignInAccount googleSignInAccount = GoogleSignIn.getLastSignedInAccount(this);
        if(googleSignInAccount != null){
            userEmail = googleSignInAccount.getEmail();
            userName = googleSignInAccount.getDisplayName();
            Toast.makeText(this, "Witaj " + userName, Toast.LENGTH_LONG).show();
        }

        googleApiClient = new GoogleApiClient.Builder(this)
                .addApi(Auth.GOOGLE_SIGN_IN_API)
                .build();

        firebaseAuth = FirebaseAuth.getInstance();
        rootRef = FirebaseFirestore.getInstance();

        authStateListener = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();
                if(firebaseUser == null){
                    Intent intent = new Intent(MainActivity.this, LoginActivity.class);
                    startActivity(intent);
                }
            }
        };

    private void signOut(){
        Map<String> map = new HashMap<>();
        map.put("tokenId", FieldValue.delete());

        rootRef.collection("users").document(userEmail).update(map).addOnSuccessListener(new OnSuccessListener<Void>(){
            @Override
            public void onSuccess(Void aVoid){
                firebaseAuth.signOut();

                if(googleApiClient.isConnected()){
                    Auth.GoogleSignInApi.signOut(googleApiClient);
                }
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        googleApiClient.connect();
        firebaseAuth.addAuthStateListener(authStateListener);
    }

    @Override
    protected void onStop() {
        super.onStop();
        if(googleApiClient.isConnected()){
            googleApiClient.disconnect();
        }
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.sign_out_button:
                signOut();
                return true;
            case R.id.action_exit:
                System.exit(0);
                return true;

            default:
                return super.onOptionsItemSelected(item);
        }
    }

这里是 loginActivity(有效):https://www.paste.org/90992

提前感谢您的所有提示!

编辑:我在这里添加了代码中最重要的部分,但我无法添加全部。

请尝试以下代码:

private void signOut(){
    Map<String> map = new HashMap<>();
    map.put("tokenId", FieldValue.delete());

    rootRef.collection("users").document(userEmail).update(map).addOnSuccessListener(new OnSuccessListener<Void>(){
        @Override
        public void onSuccess(Void aVoid){

            Auth.GoogleSignInApi.signOut(googleApiClient).addOnCompleteListener(new OnCompleteListener<Void>() {
                public void onComplete(@NonNull Task<Void> task) {
                    if (googleApiClient.isConnected()) {
                        googleApiClient.disconnect();
                        googleApiClient.connect();
                    }
                }
            });
        }

});
    }

您出现此行为是因为您的数据库是空的。使用这行代码时:

rootRef.collection("users").document(userEmail).update(map).addOnSuccessListener(/* ... */)

您正在注销,仅当令牌已成功更新时。由于您的数据库中没有任何数据,因此令牌永远不会更新,因此永远不会触发 onComplete() 方法。要解决此问题,如果不需要令牌,请使用以下代码:

private void signOut(){
    firebaseAuth.signOut();

    if(googleApiClient.isConnected()){
        Auth.GoogleSignInApi.signOut(googleApiClient);
    }
}

或者卸载该应用程序并再次 sing-in 以在您的数据库中生成用户。这样肯定能解决问题。