Whenever I Open App And Login It Works Fine And Also I Can Easily Logout From App And It Sends Me Back To Login Activity.. But When I Close The App After Login And Reopen App And Then I Logout It Sends Me To Login Activity Then App Closed. Don't Know Why.
Here's The Screenshot of Logcat
Here's The Code Which Causes Problem 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) {
}
CodePudding user response:
Most likely your security rules require that the user is signed in before they can read their own document from /users/$uid
. When you log the user out, that condition is no longer met - so your onEvent
is called with an error.
The first problem to fix is to handle the error
that is passed to onEvent
. The onEvent
always gets called with either a DocumentSnapshot
or an error, and you need to handle both cases. For example:
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);
}
}
To prevent this error from occurring in the first place, you'll want to remove the listener before you log the user out.