I tried both of those code snippets but none of them seem to work
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
firebaseAuth = FirebaseAuth.getInstance();
mAuthListener = new FirebaseAuth.AuthStateListener(){
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth){
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if(user==null){
Intent intent = new Intent(MainActivity.this, login.class);
startActivity(intent);
finish();
}
}
};
}
and this one:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user == null) {
Intent intent = new Intent(MainActivity.this, login.class);
startActivity(intent);
finish();
}
}
There are some cases where getCurrentUser will return a non-null FirebaseUser but the underlying token is not valid. This can happen, for example, if the user was deleted on another device and the local token has not refreshed. In this case, you may get a valid user getCurrentUser but subsequent calls to authenticated resources will fail.
getCurrentUser might also return null because the auth object has not finished initializing.
If you attach an AuthStateListener you will get a callback every time the underlying token state changes. This can be useful to react to edge cases like those mentioned above.
when my app starts then the main activity starts it should check if there is a logged user if not it should redirect to login activity
the problem is when I sign out from the app then close the app and reopen it , it starts from main activity