82

Currently I'm starting a new Activity and calling finish on a current one.

Is there any flag that can be passed to Intent that enables finishing current Activity without a need to call finish manually from code?

pixel
  • 24,905
  • 36
  • 149
  • 251

3 Answers3

135

You can use finish() method or you can use:

android:noHistory="true"

And then there is no need to call finish() anymore.

<activity android:name=".ClassName" android:noHistory="true" ... />
Simon Dorociak
  • 33,374
  • 10
  • 68
  • 106
  • 7
    are you sure this does what you say? from android docs: FLAG_ACTIVITY_NO_HISTORY If set, the new activity is not kept in the history stack. As soon as the user navigates away from it, the activity is finished. This may also be set with the noHistory attribute. – 10s Jul 03 '12 at 12:01
  • 28
    android:noHistory="true" is NOT the same as finish()! – Marian Paździoch Jun 08 '15 at 09:29
  • 3
    Just use the finish(), don't use noHistory – Neto Marin May 14 '17 at 00:03
  • 1
    this is perfect, thank you! – renatoarg Feb 04 '21 at 21:13
  • I have noticed 'noHistory' prevents the app from storing the screen entirely, meaning when the user minimises the app and re-opens it flings the user back to the starting activity. Finish() is better because the open screen is still cached for minimisation – Rowan Berry May 30 '22 at 06:09
100

Use finish like this:

Intent i = new Intent(Main_Menu.this, NextActivity.class);
finish();  //Kill the activity from which you will go to next activity 
startActivity(i);

FLAG_ACTIVITY_NO_HISTORY you can use in case for the activity you want to finish. For exampe you are going from A-->B--C. You want to finish activity B when you go from B-->C so when you go from A-->B you can use this flag. When you go to some other activity this activity will be automatically finished.

To learn more on using Intent.FLAG_ACTIVITY_NO_HISTORY read: http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_NO_HISTORY

Avi Kumar
  • 4,403
  • 8
  • 36
  • 67
  • 3
    FLAG_ACTIVITY_NO_HISTORY does the opposite. New activity is not saved in history, whereas I want previous Activity being finished. – pixel Jul 03 '12 at 14:07
  • 15
    doesn't `finish()` suppose to kill the current activity ? how do you expect it to reach the next line `startActivity(i)` ? Looks dangerous and unexpected to me – Michael May 31 '14 at 10:40
  • 7
    @Michael Calls are async startActivity() will get called. – tomi Jul 18 '14 at 13:46
  • 3
    @tomi could you explain this "Calls are async startActivity() will get called." more precisely? – Marian Paździoch Aug 13 '14 at 09:14
  • 1
    you can refer this answer http://stackoverflow.com/questions/10847526/what-exactly-activity-finish-method-is-doing – Avi Kumar Aug 18 '14 at 12:47
6

FLAG_ACTIVITY_NO_HISTORY when starting the activity you wish to finish after the user goes to another one.

http://developer.android.com/reference/android/content/Intent.html#FLAG%5FACTIVITY%5FNO%5FHISTORY

10s
  • 1,699
  • 1
  • 12
  • 14
  • 2
    FLAG_ACTIVITY_NO_HISTORY does the opposite. New activity is not saved in history, whereas I want previous Activity being finished. – pixel Jul 03 '12 at 14:08
  • 3
    Yes, that is what I said. Activity A you wish to finish, Activity B that is accessed from activity A. Start the activity A with the flag I mention so when the user roams away from activity a to activity B the activity A is finished. As I mentioned in the answer: "...when starting the activity you wish to finish...". It is the same answer as the confirmed one. – 10s Jul 03 '12 at 17:27