gpt4 book ai didi

android - 尝试几次后退出 Firebase

转载 作者:太空狗 更新时间:2023-10-29 13:55:23 26 4
gpt4 key购买 nike

我正在使用 Firebase,我在 onBackPressed() 中添加了 signOut() 方法以返回登录 Activity 。但是当我按“后退”以退出时,我返回到登录 Activity 并且 onAuthStateChanged 在登录后将我带回下一个 Activity 。不知何故我仍然登录。

当我按下后退按钮时,它会在第二次或第三次后起作用。这真的很奇怪,我希望你能帮助我..这是我的一些代码 fragment :

这是登录后 Activity 中的onBackPressed():

@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();

FirebaseAuth.getInstance().signOut();
finish();

}
}

这是登录 Activity 中的AuthStateListener():

  mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
curr_user = user;

Toast.makeText(LoginActivity.this, "yes", Toast.LENGTH_SHORT).show();
if(curr_user.getDisplayName() == null)
autoSignIn("email");
else
autoSignIn("google");

user.getUid());
} else {
Toast.makeText(LoginActivity.this, "out", Toast.LENGTH_SHORT).show();
}
}
};

这是启动下一个 Activity 的 autoSignIn() 方法:

public void autoSignIn(String accType) {

if(accType.equals("email")) {

mDatabaseReference.child("users").orderByKey().equalTo(curr_user.getUid());

Query myTopPostsQuery = mDatabaseReference.child("users").orderByKey().equalTo(curr_user.getUid());
myTopPostsQuery.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {

editor = pref.edit();
User user = snapshot.getValue(User.class);
editor.putString("DisplayName", user.username);
editor.commit();
intent = new Intent(getApplicationContext(), MainActivity.class);
intent.putExtra("userID", curr_user.getUid());
startActivity(intent);
}

}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});
}
else {
Toast.makeText(this,"google", Toast.LENGTH_SHORT).show();

txtWelcome.setText("Hello " + curr_user.getDisplayName());
intent = new Intent(getApplicationContext(), MainActivity.class);
intent.putExtra("userID", curr_user.getUid());
editor = pref.edit();
editor.putString("DisplayName", curr_user.getDisplayName());
editor.commit();
startActivity(intent);

}
}

最佳答案

终于明白了,在 FirebaseAuth.getInstance().signOut();finish() 之后,当 user !=空。我删除了对方法 autoSignIn() 的调用,它起作用了。

关于android - 尝试几次后退出 Firebase,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40143301/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com