I have an application with several Activities in Android and I want the user to be able to log-out by pressing a menu button. The problem I have is that
A) Android doesn't let you terminate the application and
B) even when I send the user to the LoginActivity
again they can always press back and get right back to the previous activity they were in.
I already tried to launch the Activity with the two following flags:
Intent intent = new Intent(this, LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
I also tried with each one of them by themselves.
I also tried calling finish()
after startActivity(intent)
as I read in another StackOverflow
question.
In your login activity, override the back button, so it hides your app instead of finishing the activity:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
moveTaskToBack(true);
return true;
}
return super.onKeyDown(keyCode, event);
}
Also be sure to set android:alwaysRetainTaskState="true" on the root activity, so Android doesn't clear your stack (including the login activity) after 30min of inactivity from user.
Then just call finish() when there is a successful login.