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?

1
  • Check this answer, it was useful for me.
    – Daniyar
    Mar 12, 2019 at 14:00

11 Answers 11

260

You just need to call finish()

Intent intent = new Intent(this, NextActivity.class);
startActivity(intent);
finish();
8
  • 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, 2010 at 3:02
  • @CaseyB thanks for this..but i want to know, can i call this method from a Service if yes then how?
    – swiftBoy
    Jun 6, 2012 at 6: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 6, 2012 at 13:53
  • 3
    Its a 7 years old answer, and yet still valid for Android M and N.
    – zeeshan
    Mar 22, 2017 at 14:24
  • 1
    @juztcode onDestroy is a lifecycle method that is only meant to be called from the Android framework.
    – CaseyB
    Dec 18, 2019 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

11

you can use:

intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
1
9

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

<activity
            ...
            android:noHistory="true">
</activity>
6

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

3

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

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

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 .

3
  • 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, 2020 at 0:09
  • 2
    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. Apr 14, 2020 at 8:55
  • This is what I needed. Thank you. I didn't know how to end the process from a menu. Nov 25, 2023 at 20:31
0

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

0
0

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

-1

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

startActivity(new Intent(this, newactivity.class));
finish();
-1

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()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.