There's this android application I'm building with Firebase as the backend. It requires two different sets of users, a set of lecturers and a set of students. There's an issue however. I would want a student that has already logged in and closed the app to automatically log in to a different home activity when the student opens the app again. Same should apply to the lecturers. How do I achieve that? Can someone help with a sample code? I know about the functionality that firebase uses to automatically log in users to the homepage of an app but how do I specify the page that should automatically open if one is a student or a lecturer?
My Login Activity
public class LoginActivity extends AppCompatActivity {
private TextInputLayout mLoginEmail;
private TextInputLayout mLoginPassword;
private ProgressDialog mLoginProgress;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
private DatabaseReference jLoginDatabase, student_token_reference, lecturer_token_reference;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
mLoginProgress = new ProgressDialog(this);
mAuth = FirebaseAuth.getInstance();
if (mAuth.getCurrentUser() != null) {
Intent main_intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(main_intent);
}
mLoginEmail = findViewById(R.id.login_email);
mLoginPassword = findViewById(R.id.login_password);
Button mLogin_btn = findViewById(R.id.login_btn);
TextView msignup = findViewById(R.id.sign_up_text);
msignup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent gotosignup = new Intent(LoginActivity.this, ChoiceActivity.class);
startActivity(gotosignup);
}
});
mLogin_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String email = mLoginEmail.getEditText().getText().toString();
String password = mLoginPassword.getEditText().getText().toString();
if (!TextUtils.isEmpty(email) || !TextUtils.isEmpty(password)) {
mLoginProgress.setTitle("Logging in user");
mLoginProgress.setMessage("Please wait while we log you in...");
mLoginProgress.setCanceledOnTouchOutside(false);
mLoginProgress.show();
loginUser(email, password);
} else {
Toast.makeText(LoginActivity.this, "Please fill in credentials first", Toast.LENGTH_LONG).show();
}
}
});
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
} else {
// User is signed out
}
// ...
}
};
}
private void loginUser(String email, String password) {
mAuth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
// If sign in fails, display a message to the user. If sign in succeeds
// the auth state listener will be notified and logic to handle the
// signed in user can be handled in the listener.
if (task.isSuccessful()) {
FirebaseUser currentUser = FirebaseAuth.getInstance().getCurrentUser();
String RegisteredUserID = currentUser.getUid();
jLoginDatabase = FirebaseDatabase.getInstance().getReference().child("Users").child(RegisteredUserID);
jLoginDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String userType = dataSnapshot.child("userType").getValue().toString();
if (userType.equals("Lecturers")) {
mLoginProgress.dismiss();
Intent intentResident = new Intent(LoginActivity.this, LecturerMainActivity.class);
intentResident.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intentResident);
finish();
} else if (userType.equals("Students")) {
mLoginProgress.dismiss();
Intent intentMain = new Intent(LoginActivity.this, MainActivity.class);
intentMain.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intentMain);
finish();
} else {
mLoginProgress.hide();
Toast.makeText(LoginActivity.this, "Failed to authenticate user", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
});
}
@Override
protected void onStart() {
super.onStart();
}
@Override
protected void onStop() {
super.onStop();
}
@Override
public void onBackPressed() {
moveTaskToBack(true);
super.onBackPressed();
}
}
