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

Android

Android Problem Overview


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?

Android Solutions


Solution 1 - Android

You just need to call finish()

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

Solution 2 - Android

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

Solution 3 - Android

you can use:

intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

Solution 4 - Android

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

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

Solution 5 - Android

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

Solution 6 - Android

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

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

Solution 7 - Android

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 .

Solution 8 - Android

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

Solution 9 - Android

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

Solution 10 - Android

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

Solution 11 - Android

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

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJohnRockView Question on Stackoverflow
Solution 1 - AndroidCaseyBView Answer on Stackoverflow
Solution 2 - AndroidjqpubliqView Answer on Stackoverflow
Solution 3 - AndroidJmorvanView Answer on Stackoverflow
Solution 4 - AndroidDanke XieView Answer on Stackoverflow
Solution 5 - AndroidRobGThaiView Answer on Stackoverflow
Solution 6 - AndroidJBL AK-47View Answer on Stackoverflow
Solution 7 - AndroidiamkdblueView Answer on Stackoverflow
Solution 8 - AndroidSoftwareGuyView Answer on Stackoverflow
Solution 9 - AndroidDayoView Answer on Stackoverflow
Solution 10 - AndroidDevang ShahView Answer on Stackoverflow
Solution 11 - AndroidSoni KumarView Answer on Stackoverflow