title: How do you use Intent.FLAG_ACTIVITY_CLEAR_TOP to clear the Activity Stack?
body: <p>I've read through several posts about using this but must be missing something as it's not working for me. My activity A has launchmode=&quot;singleTop&quot; in the manifest. It starts activity B, with launchmode=&quot;singleInstance&quot;. Activity B opens a browser and receives an intent back, which is why it's singleInstance. I'm trying to override the back button so that the user is sent back to activity A, and can then press Back to leave the activity, rather than back to activity B again.</p>
  <pre><code>// activity B
  @Override
  public boolean onKeyDown(int keyCode, KeyEvent event)  {
   if (android.os.Build.VERSION.SDK_INT &lt; android.os.Build.VERSION_CODES.ECLAIR
    &amp;&amp; keyCode == KeyEvent.KEYCODE_BACK
    &amp;&amp; event.getRepeatCount() == 0) onBackPressed();
   return super.onKeyDown(keyCode, event);
  }
  @Override
  public void onBackPressed() {
   startActivity(new Intent(this, UI.class)
   .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK));
   return;
  }
  </code></pre>
  <p>After returning from the browser, the stack is...
  A,B,Browser,B</p>
  <p>I expect this code to change the stack to...
  A
  ... so that pressing back once more takes the user back to the Home Screen.</p>
  <p>Instead, it seems to change the stack to...
  A,B,Browser,B,A
  ...as though those flags aren't there.</p>
  <p>I tried calling finish() in activity B after startActivity, but then the back button takes me back to the browser again!</p>
  <p>What am I missing?</p>