Android应用程序使用Firebase打开多次
因此,我已经挠头好几个小时了,我在我的android应用程序中使用Firebase身份验证,出于某种原因,它一直随机出现,基本上会打开多次,在研究后,我发现它可能就是AuthStateListener的方法 我曾尝试添加一个类似于有相同问题的人的标志,但没有效果。如果Firebase用户或身份验证不等于null,而应用程序本身没有自动打开,我应该从哪里开始我的活动。非常感谢您的帮助 这是我的密码:Android应用程序使用Firebase打开多次,android,authentication,firebase,firebase-authentication,Android,Authentication,Firebase,Firebase Authentication,因此,我已经挠头好几个小时了,我在我的android应用程序中使用Firebase身份验证,出于某种原因,它一直随机出现,基本上会打开多次,在研究后,我发现它可能就是AuthStateListener的方法 我曾尝试添加一个类似于有相同问题的人的标志,但没有效果。如果Firebase用户或身份验证不等于null,而应用程序本身没有自动打开,我应该从哪里开始我的活动。非常感谢您的帮助 这是我的密码: mAuthListner= new FirebaseAuth.AuthStateListener(
mAuthListner= new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
if(firebaseAuth.getCurrentUser()!=null){firebaseAuth= FirebaseAuth.getInstance();
//Log.i("User1",firebaseAuth.getCurrentUser().toString());
FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();
if(!isMyServiceRunning(MyService.class))
startService(new Intent(getApplicationContext(), MyService.class));
//prefManager = new PrefManager(getApplicationContext());
if (MyPreferences.isFirst(getApplicationContext()) ||!databaseOperations.checkUser(databaseOperations)) {
launchHomeScreen();
}else {
if(flag){
startActivity(new Intent(MainActivity.this, MainDrawer.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK));
finish();
}
flag=false;
}
}
}
};
当活动停止时,您可能没有删除侦听器。如果您不删除它,它将保持注册状态并侦听身份验证事件。身份验证令牌刷新(每小时自动进行一次)可能会触发侦听器
通常,您可以在onStart期间添加侦听器,并在onStop期间删除它们。始终与添加同时删除。@Override protected void onStop(){super.onStop();auth.removeAuthStateListener(mAuthListner);}