I just recently learned how to clear the backstack in Android. I have two activities, one for login (LoginActivity) and one to use the application (MainActivity). It consists of a bunch of fragments. This is the code I used to start the MainActivity
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish(); // call this to finish the current activity
Everything works fine, when I'm on the MainActivity and I press the home
button, the application closes. When I open it back up, it opens the MainActivity. But when I press the back
button, it's closing the application and when I open it back up, the LoginActivity is opening. How do I override the back
button so it behaves the same as the home
button.
Sounds like you want to override the back button behavior to move the app to the background instead of doing the usual back button behavior.
You can do this by overriding onBackPressed
:
@Override
public void onBackPressed()
{
moveTaskToBack(true);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With