Firebase - 当用户在重新打开应用程序后注销时应用程序关闭
Firebase - App Shuts Down When User Log Out After ReOpening App
每当我打开应用程序并登录时,它工作正常,而且我可以轻松地从应用程序中注销,它会让我返回登录 Activity..但是当我在登录后关闭应用程序并重新打开应用程序然后我注销它让我登录 Activity 然后应用程序关闭。不知道为什么。
这是 Logcat
的屏幕截图
这是导致问题的代码
SignupTabFragment.java
if(firebaseAuth.getCurrentUser() != null){
// Retrieving User Data From Firebase
userIDFireStore = firebaseAuth.getCurrentUser().getUid();
DocumentReference documentReference = firestore.collection("users").document(userIDFireStore);
documentReference.addSnapshotListener(new EventListener<DocumentSnapshot>() {
@Override
public void onEvent(@Nullable DocumentSnapshot value, @Nullable FirebaseFirestoreException error) {
userTypeFirestore = value.getString("userType");
if(userTypeFirestore.equals("Admin")){
startActivity(new Intent(getActivity().getApplicationContext(),AdminHomeActivity.class));
getActivity().finish();
}
if(userTypeFirestore.equals("Student")){
startActivity(new Intent(getActivity().getApplicationContext(),HomeActivity.class));
getActivity().finish();
}
}
});
}
HomeActivity.java
public class HomeActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
}
public void onLogoutClick(View view) {
// Logout
FirebaseAuth.getInstance().signOut();
startActivity(new Intent(getApplicationContext(),LoginActivity.class));
finish();
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0){
finish();
System.exit(0);
}
return super.onKeyDown(keyCode, event);
}
}
LoginActivity.java
public class LoginActivity extends AppCompatActivity {
TabLayout tabLayout;
TabItem tabLogin,tabSignup;
ViewPager viewPager;
int nightModeFlags;
ConstraintLayout constraintLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLogin = (TabItem) findViewById(R.id.tab_login);
tabSignup = (TabItem) findViewById(R.id.tab_signup);
viewPager = (ViewPager) findViewById(R.id.view_pager);
constraintLayout = (ConstraintLayout) findViewById(R.id.constraint_backgrond);
// To Change Constraint Layout Background in Night Mode
nightModeFlags = constraintLayout.getContext().getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
switch(nightModeFlags){
case Configuration.UI_MODE_NIGHT_YES:
constraintLayout.setBackgroundResource(R.drawable.view_bg_night);
break;
case Configuration.UI_MODE_NIGHT_NO:
constraintLayout.setBackgroundResource(R.drawable.view_bg);
break;
case Configuration.UI_MODE_NIGHT_UNDEFINED:
constraintLayout.setBackgroundResource(R.drawable.view_bg);
break;
}
// tabLayout.addTab(tabLayout.newTab().setText("Login"));
// tabLayout.addTab(tabLayout.newTab().setText("Sign Up"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
final LoginAdapter adapter = new LoginAdapter(getSupportFragmentManager(), this,tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
// Changing Fragments When The Tab is Selected or Clicked.
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
您的安全规则很可能要求用户先登录,然后才能从 /users/$uid
阅读自己的文档。当您注销用户时,不再满足该条件 - 因此调用您的 onEvent
时出现错误。
要解决的第一个问题是处理传递给 onEvent
的 error
。 onEvent
总是被调用 或者 一个 DocumentSnapshot
或者一个错误,你需要处理这两种情况。例如:
public void onEvent(@Nullable DocumentSnapshot value, @Nullable FirebaseFirestoreException error) {
if (error == null) {
userTypeFirestore = value.getString("userType");
if(userTypeFirestore.equals("Admin")){
startActivity(new Intent(getActivity().getApplicationContext(),AdminHomeActivity.class));
getActivity().finish();
}
if(userTypeFirestore.equals("Student")){
startActivity(new Intent(getActivity().getApplicationContext(),HomeActivity.class));
getActivity().finish();
}
}
else {
Log.e("Firestore", "Error listening to user data", error);
}
}
首先要防止此错误发生,您需要 remove the listener 在注销用户之前。
每当我打开应用程序并登录时,它工作正常,而且我可以轻松地从应用程序中注销,它会让我返回登录 Activity..但是当我在登录后关闭应用程序并重新打开应用程序然后我注销它让我登录 Activity 然后应用程序关闭。不知道为什么。
这是 Logcat
的屏幕截图这是导致问题的代码 SignupTabFragment.java
if(firebaseAuth.getCurrentUser() != null){
// Retrieving User Data From Firebase
userIDFireStore = firebaseAuth.getCurrentUser().getUid();
DocumentReference documentReference = firestore.collection("users").document(userIDFireStore);
documentReference.addSnapshotListener(new EventListener<DocumentSnapshot>() {
@Override
public void onEvent(@Nullable DocumentSnapshot value, @Nullable FirebaseFirestoreException error) {
userTypeFirestore = value.getString("userType");
if(userTypeFirestore.equals("Admin")){
startActivity(new Intent(getActivity().getApplicationContext(),AdminHomeActivity.class));
getActivity().finish();
}
if(userTypeFirestore.equals("Student")){
startActivity(new Intent(getActivity().getApplicationContext(),HomeActivity.class));
getActivity().finish();
}
}
});
}
HomeActivity.java
public class HomeActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
}
public void onLogoutClick(View view) {
// Logout
FirebaseAuth.getInstance().signOut();
startActivity(new Intent(getApplicationContext(),LoginActivity.class));
finish();
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0){
finish();
System.exit(0);
}
return super.onKeyDown(keyCode, event);
}
}
LoginActivity.java
public class LoginActivity extends AppCompatActivity {
TabLayout tabLayout;
TabItem tabLogin,tabSignup;
ViewPager viewPager;
int nightModeFlags;
ConstraintLayout constraintLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLogin = (TabItem) findViewById(R.id.tab_login);
tabSignup = (TabItem) findViewById(R.id.tab_signup);
viewPager = (ViewPager) findViewById(R.id.view_pager);
constraintLayout = (ConstraintLayout) findViewById(R.id.constraint_backgrond);
// To Change Constraint Layout Background in Night Mode
nightModeFlags = constraintLayout.getContext().getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
switch(nightModeFlags){
case Configuration.UI_MODE_NIGHT_YES:
constraintLayout.setBackgroundResource(R.drawable.view_bg_night);
break;
case Configuration.UI_MODE_NIGHT_NO:
constraintLayout.setBackgroundResource(R.drawable.view_bg);
break;
case Configuration.UI_MODE_NIGHT_UNDEFINED:
constraintLayout.setBackgroundResource(R.drawable.view_bg);
break;
}
// tabLayout.addTab(tabLayout.newTab().setText("Login"));
// tabLayout.addTab(tabLayout.newTab().setText("Sign Up"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
final LoginAdapter adapter = new LoginAdapter(getSupportFragmentManager(), this,tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
// Changing Fragments When The Tab is Selected or Clicked.
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
您的安全规则很可能要求用户先登录,然后才能从 /users/$uid
阅读自己的文档。当您注销用户时,不再满足该条件 - 因此调用您的 onEvent
时出现错误。
要解决的第一个问题是处理传递给 onEvent
的 error
。 onEvent
总是被调用 或者 一个 DocumentSnapshot
或者一个错误,你需要处理这两种情况。例如:
public void onEvent(@Nullable DocumentSnapshot value, @Nullable FirebaseFirestoreException error) {
if (error == null) {
userTypeFirestore = value.getString("userType");
if(userTypeFirestore.equals("Admin")){
startActivity(new Intent(getActivity().getApplicationContext(),AdminHomeActivity.class));
getActivity().finish();
}
if(userTypeFirestore.equals("Student")){
startActivity(new Intent(getActivity().getApplicationContext(),HomeActivity.class));
getActivity().finish();
}
}
else {
Log.e("Firestore", "Error listening to user data", error);
}
}
首先要防止此错误发生,您需要 remove the listener 在注销用户之前。