@Override
protected void onResume() {
super.onResume();
handler = new Handler();
authCheckRunnable = new Runnable() {
@Override
public void run() {
checkAuthState();
handler.postDelayed(this, 1000); // Check every 1 second
}
};
handler.post(authCheckRunnable);
}
@Override
protected void onPause() {
super.onPause();
if (handler != null && authCheckRunnable != null) {
handler.removeCallbacks(authCheckRunnable);
}
if (authStateListener != null) {
firebaseAuth.removeAuthStateListener(authStateListener);
}
}
private void checkAuthState() {
authStateListener = firebaseAuth -> {
if (firebaseAuth.getCurrentUser() != null) {
firebaseAuth.getCurrentUser().reload();
firebaseAuth.getCurrentUser().reauthenticate()
if (firebaseAuth.getCurrentUser().isEmailVerified()) {
if (openMailDialog != null) openMailDialog.dismiss();
firebaseAuth.removeAuthStateListener(authStateListener);
startActivity(new Intent(RegisterActivity.this, MainActivity.class));
finish();
}
}
};
firebaseAuth.addAuthStateListener(authStateListener);
}