Friday, April 27, 2012

How to kill an Android activity when leaving it so that it cannot be accessed from the back button?


In an given Android activity, I would like to start a new activity for the user at some point. Once they leave the first activity and arrive at the second, the first activity is stale and I want to remove it completely so it can not be accessed again from the back button.



How is the best way to accomplish this? How do I kill or destroy this activity immediately after the user has launched the new activity?


Source: Tips4all

4 comments:

  1. You just need to call finish()

    Intent intent = new Intent(this, NextActivity.class);
    startActivity(intent);
    finish();

    ReplyDelete
  2. Setting android:noHistory="true" on the activity in your manifest will remove an activity from the stack whenever it is navigated away from. see here

    ReplyDelete
  3. Yes, all you need to do is call finish() in any Activity you would like to close.

    ReplyDelete
  4. write this in each "new activity" after you initialized your new intent->

    Intent i = new Intent(this, yourClass.class);
    startActivity(i);
    finish();


    FTI-UKSW

    ReplyDelete