Overriding default back button behavior in android
<p>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</p>

<pre><code>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
</code></pre>

<p>Everything works fine, when I'm on the MainActivity and I press the <code>home</code> button, the application closes. When I open it back up, it opens the MainActivity. But when I press the <code>back</code> button, it's closing the application and when I open it back up, the LoginActivity is opening. How do I override the <code>back</code> button so it behaves the same as the <code>home</code> button. </p>