I want to separate my admin user and student user, so I've created a key on my Firestore database named 'userType', and I've used fragments to make a login and signup form. In signup form, I've fetched data, and when I login initially, it works fine. However, when I reopen my app, it just crashes. I've written code in signup form for sending logged in users to different activities according to their user type. I edited one key as admin in Firestore.
SignupTabFragment, here's the problem.
package com.example.mkmaterials;
public class SignupTabFragment extends Fragment {
public static final String TAG = "TAG";
EditText signupName,signupEmail,signupPassword,signupConfirmPassword;
boolean signupPasswordVisible,signupRepeatPasswordVisible;
Button signup;
private FirebaseAuth firebaseAuth;
private FirebaseFirestore firestore;
String signupUserType = "Student";
String userID;
private String userIDFireStore,userTypeFirestore;
ProgressBar signupProgressBar;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.signup_tab_fragment,container,false);
signupName = root.findViewById(R.id.edt_SignupName);
signupEmail = root.findViewById(R.id.edt_SignupEmail);
signupPassword = root.findViewById(R.id.edt_SignupPassword);
signupConfirmPassword = root.findViewById(R.id.edt_SignupConfirmPassword);
signup = root.findViewById(R.id.btn_Signup);
firebaseAuth = FirebaseAuth.getInstance();
firestore = FirebaseFirestore.getInstance();
signupProgressBar = root.findViewById(R.id.progress_SignupProgressBar);
This is The Problematic Area
if(firebaseAuth.getCurrentUser() != null){
// Retrieving User Data From Firebase
userIDFireStore = firebaseAuth.getCurrentUser().getUid();
DocumentReference documentReference = firestore.collection("users").document(userIDFireStore);
documentReference.addSnapshotListener(getActivity(), 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();
}
}
});
}