退出不起作用。单击时没有任何反应。有没有人看到错误?
代码太长了 - 这是我的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
提前感谢您提供所有提示!
编辑:我在这里添加了代码中最重要的部分,但我无法添加整个部分。答案 0 :(得分:1)
请尝试以下代码:
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();
}
}
});
}
});
}
答案 1 :(得分:1)
您收到此行为是因为您的数据库为空。使用这行代码时:
rootRef.collection("users").document(userEmail).update(map).addOnSuccessListener(/* ... */)
只有在令牌成功更新时才退出。由于数据库中没有任何数据,因此永远不会更新令牌,因此永远不会触发onComplete()
方法。要解决此问题,请在不需要令牌的情况下使用以下代码:
private void signOut(){
firebaseAuth.signOut();
if(googleApiClient.isConnected()){
Auth.GoogleSignInApi.signOut(googleApiClient);
}
}
或者卸载应用程序并再次进行歌唱以在数据库中生成用户。这肯定会解决问题。