146

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?

JohnRock
  • 6,795
  • 15
  • 52
  • 61
  • Check this [answer](https://stackoverflow.com/questions/7075349/android-clear-activity-stack), it was useful for me. – Daniyar Mar 12 '19 at 14:00

11 Answers11

259

You just need to call finish()

Intent intent = new Intent(this, NextActivity.class);
startActivity(intent);
finish();
CaseyB
  • 24,780
  • 14
  • 77
  • 112
  • Thank you. To take it one step further, I am starting the new activity from a dialogue window - would the code work the same way? – JohnRock Mar 31 '10 at 03:02
  • @CaseyB thanks for this..but i want to know, can i call this method from a Service if yes then how? – swiftBoy Jun 06 '12 at 06:13
  • 1
    Services don't have an Activity which means that they won't be in the activity stack to go back to. If you want to kill some activity from your service you should be able to call myActivity.finish(); – CaseyB Jun 06 '12 at 13:53
  • I am new and I dont understand "this" in your code. Please axplain it for me. Why did you use it there? – ffttyy Aug 20 '15 at 19:25
  • @ffttyy The code might be written as `Intent intent = new Intent(MyActivity.this, StreamActivity.class);`. Here, the MyActivity is the class which you are about to leave. In that case you should call startActivity() method like this `MyActivity.this.startActivity(intent)`. _this_ is only a reference to the current object. So your intent is to move from this activity to the NextActivity. – Onur Tuna Jul 26 '16 at 07:10
  • 3
    Its a 7 years old answer, and yet still valid for Android M and N. – zeeshan Mar 22 '17 at 14:24
  • @CaseyB , what if you call the `onDestroy` method from the `onPause` method? – juztcode Dec 14 '19 at 03:51
  • 1
    @juztcode `onDestroy` is a lifecycle method that is only meant to be called from the Android framework. – CaseyB Dec 18 '19 at 18:02
111

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

jqpubliq
  • 11,874
  • 2
  • 34
  • 26
11

you can use:

intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
Jmorvan
  • 1,020
  • 11
  • 31
9

You can also add android:noHistory="true" to your Activity tag in AndroidManifest.xml.

<activity
            ...
            android:noHistory="true">
</activity>
Danke Xie
  • 1,757
  • 17
  • 13
6

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

RobGThai
  • 5,937
  • 8
  • 41
  • 59
3

Write this in each "new activity" after you initialized your new intent->

Intent i = new Intent(this, yourClass.class);
startActivity(i);
finish();
jonsca
  • 10,218
  • 26
  • 54
  • 62
JBL AK-47
  • 39
  • 1
2

Finally, I got a solution!

My Context is:- I want disconnect socket connection when activity destroyed, I tried to finish() activity but it didn't work me, its keep connection live somewhere.

so I use android.os.Process.killProcess(android.os.Process.myPid()); its kill my activity and i used android:excludeFromRecents="true" for remove from recent activity .

iamkdblue
  • 3,448
  • 2
  • 25
  • 43
  • 1
    This solution worked for me but unfortunately shows a blank page and to close socket connection you can use socket.close(); . – Mehrdad Mar 17 '20 at 00:09
  • 1
    `android.os.Process.killProcess(android.os.Process.myPid())` kills the app process and not only the activity. This means that any ongoing service (background or foreground) will be stopped as well. – Wrichik Basu Apr 14 '20 at 08:55
0

Add this attribute to you activity in manifest file. android:noHistory="true"

SoftwareGuy
  • 1,121
  • 2
  • 11
  • 23
-1

You've below options to remove an activity from the back stack for a particular task.

  1. Call finish() method just after startActivity() like this:

         startActivity(new Intent(FirstActivity.this, SecondActivity.class));
         finish();
    
  2. Add an Intent flag like so:

         startActivity(new Intent(FirstActivity.this, SecondActivity.class).addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY));
    
  3. Add noHistory=true in your manifest file for that particular activity

-2

You just need to use below code when launching the new activity.

startActivity(new Intent(this, newactivity.class));
finish();
Soni Kumar
  • 283
  • 1
  • 4
  • 16
-2

In Kotlin, you may use this

startActivity(Intent(context, newActivity::class.java))
finish()

Or you can use this also

val intent = Intent(context, newActivity::class.java))
startActivity(intent)
finish()
Dayo
  • 39
  • 1
  • 6