Close application and remove from recent apps/

60,404

Solution 1

I based my solution on guest's above, as well as gilsaints88's comments below (for Android L compatibility):

Add this activity to your AndroidManifest.xml file:

<activity
    android:name="com.example.ExitActivity"
    android:theme="@android:style/Theme.NoDisplay"
    android:autoRemoveFromRecents="true"/>

Then create a class ExitActivity.java:

public class ExitActivity extends Activity
{
    @Override protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        if(android.os.Build.VERSION.SDK_INT >= 21)
        {
            finishAndRemoveTask();
        }
        else
        {
            finish();
        }
    }

    public static void exitApplication(Context context)
    {
        Intent intent = new Intent(context, ExitActivity.class);

        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);

        context.startActivity(intent);
    }
}

Then whenever you want to force close your application and remove it from the recent's list, call:

ExitActivity.exitApplication(context);

This solution works for me instead of declaring android:excludeFromRecents="true" because I want the user to be able to see the app in the recents list UNTIL the point where my app triggers closing the app programmatically.

Solution 2

Before I present my attempt at this, I want to make it clear that what follows won't address the availability of "force stop" in the application info. Android allows you to force stop an application even if it does not have any processes running. Force stop puts the package into a specific stopped state, where it can't receive broadcast events.

Now that that's out of the way, on with my cheesy idea for this. The gist of it is to replace the task that you want to exit with one that the system will exclude from the recent apps list.

  1. Define an activity (I'll call it Exiter) that just finishes itself immediately.
    • Call finish() in onCreate(Bundle).
    • From the question, it sounds like the OP will also want to call System.exit(int) too. But for other people reading this answer, you usually don't have to kill the process.
    • Use android:theme="@android:style/Theme.NoDisplay" to prevent this from causing a bunch of redundant transitions.
  2. Set android:taskAffinity so that Exiter will run in the task that you want to exit. Or don't: by default, unspecified taskAffinity makes it so that all activities share a common anonymous taskAffinity.
  3. When you want to exit, use startActivity(Intent) to start Exiter with the following flags:
    • Intent.FLAG_ACTIVITY_NEW_TASK - Even though we want to start the activity in the same task, the other flags require this flag. Fortunately, the taskAffinity will prevent the system from actually starting a new task.
    • Intent.FLAG_ACTIVITY_CLEAR_TASK - This finishes all the activities on the task. You won't have to call finish() everywhere, just in Exiter.
    • Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS - The task won't be in the recent apps list. Since it's the same task as the one that's exiting, this flag removes the task from the list.

https://gist.github.com/anonymous/9884978

Solution 3

finishAndRemoveTask(); worked for me to remove an app from taskbar. After closing app.

Solution 4

Add this to your Activity:

@Override
public void finish() {
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        super.finishAndRemoveTask();
    }
    else {
        super.finish();
    }
}

finishAndRemoveTask()

Call this when your activity is done and should be closed and the task should be completely removed as a part of finishing the root activity of the task.

Solution 5

The solution for Kotlin.

ExitHelper.kt

fun Activity.finishAndRemoveTaskCompat() {
    if (android.os.Build.VERSION.SDK_INT >= 21) {
        finishAndRemoveTask()
    } else {
        val intent = Intent(this, ExitAndRemoveFromRecentAppsDummyActivity::class.java)

        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or
                Intent.FLAG_ACTIVITY_CLEAR_TASK or
                Intent.FLAG_ACTIVITY_NO_ANIMATION or
                Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS)

        startActivity(intent)
    }
}

class ExitAndRemoveFromRecentAppsDummyActivity : Activity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        finish()
    }
}

AndroidManifest.xml

<activity
     android:name=".ExitAndRemoveFromRecentAppsDummyActivity"
     android:theme="@android:style/Theme.NoDisplay"
/>

Then just use it as a usual method in your Activity:

override fun boo() {
    foo()
    finishAndRemoveTaskCompat()
}
Share:
60,404
Hamad
Author by

Hamad

I am a techno freak and i like to keep myself updated with the latest gadgets and nevertheless, latest software. technology has been developed to both simplify and modify a person's life, and i am too digging up with my work to simplify and modify other's life by my own creations.

Updated on June 10, 2021

Comments

  • Hamad
    Hamad about 3 years

    I know this question is common and asked many times on Stack Overflow, but after visiting nearly the four pages of search engine results and nearly the 20 question of Stack Overflow regarding this issue, I found that none of them is resolved or answered correctly.

    What I want:

    I want to show my app in recent apps list when it is running but when I close app then my process should be killed and application should be removed from recent apps list.

    Some answers I found:

    use System.exit(0); //doesn't clear app from recents
    OR
    use android.os.Process.killProcess(android.os.Process.myPid()); //doesn't clear app from recents
    OR
    use finish() or this.finish() or Activity.finish();// doesn't clear app from recents
    

    And one common suggestion I see in every answer that add below code in manifest:

    android:excludeFromRecents //I think this is wrong approach.
    

    because after adding this when user presses home button while my app is running then user cannot see my app in recent Apps List.

    And many other suggestions on this but none of them do both tasks close application and clear application from recent apps list. Also if you go in Settings>Apps>yourApp>your Application see that still it asks for "force stop" means application is running!

    • rajshree
      rajshree over 10 years
    • tasomaniac
      tasomaniac over 10 years
      You can use <activity android:excludeFromRecents="true" to hide the activity from the recents completely but I do not know a way to hide it after showing it. I think it is imposibble without rooting.
    • Hamad
      Hamad over 10 years
      @rajshree your link doesn't solve my question see my updated question
    • Hamad
      Hamad over 10 years
      @tasomaniac ya! that's why i think that's not the right approach to use.
  • guest
    guest about 10 years
    hm, or just specify android:excludeFromRecents="true" in the manifest entry for Exiter instead of using the flag all the time
  • gilsaints88
    gilsaints88 about 9 years
    This solution does not work anymore for API 21/22. It does not remove the app from recent documents anymore. Anyone have a solution that works for API 21/22? Thanks
  • gilsaints88
    gilsaints88 about 9 years
    Found a solution that will work for API 21 and above. replace finish(); in ExitActivity with: if (android.os.Build.VERSION.SDK_INT >= 21) { finishAndRemoveTask(); } else { finish(); }
  • Hamad
    Hamad over 8 years
    its available after API level 21 and my app targets from API level 10 to 23.
  • Hamad
    Hamad over 8 years
    its available after API level 21 and my app targets from API level 10 to 23.
  • w3bshark
    w3bshark almost 8 years
    This should be the accepted answer. Thanks for your help, Luke and gilsaints88!!! It's seriously appreciated.
  • steven smith
    steven smith over 7 years
    I wish there was a way to do this without adding an additional activity but I couldn't find one. Thank you!
  • silentsudo
    silentsudo over 6 years
    This is nice, but what if we have service running removing start also restarts the service. My service is getting restarted and i am not able to get the correct behavior now.