Remove app from recent apps programmatically
Asked Answered
O

7

65

I know that Activities can be declared in manifest as being excluded from recents with android:excludeFromRecents: http://developer.android.com/guide/topics/manifest/activity-element.html#exclude

However, that's not what I'm looking for, I would like to know if there is a way to remove the app from recent apps programmatically

Olympie answered 14/11, 2012 at 18:48 Comment(4)
possible duplicate of Exclude activity from recents menu by codeSpiral
Not exactly, since I want the whole app to be excluded from recents, and I would like to do that at any moment, not when launching an ActivityOlympie
did you ever find a solution? I'm trying to do the same thingInvestiture
I will post my solution as an answerOlympie
S
47

Yes, generally when you want to have special properties for an Activity when starting it you supply special flags to the Intent. In this case FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS.

Updated:

If you need to hide the current already running activity, you might be able to use this flag in combination with FLAG_ACTIVITY_CLEAR_TOP which would send the new Intent to the existing Activity. You'll have to think and perhaps experiment with what happens as the user moves around your stack though and whether that will make your app re-appear in the recent apps.

Spreadeagle answered 14/11, 2012 at 18:54 Comment(0)
L
27

This can be done using the ActivityManager.AppTask functionality (starting in API 21)

    ActivityManager am = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
    if(am != null) {
        List<ActivityManager.AppTask> tasks = am.getAppTasks();
        if (tasks != null && tasks.size() > 0) {
            tasks.get(0).setExcludeFromRecents(true);
        }
    }
Lysias answered 30/4, 2015 at 17:7 Comment(1)
How can I use that approach on API levels below 21?Weldonwelfare
H
26

Add these lines to the Activity from which you are exiting the application:

@Override
public void finish() {
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        super.finishAndRemoveTask();
    }
    else {
        super.finish();
    }
}
Homotaxis answered 7/12, 2017 at 5:55 Comment(0)
C
10

Following is the definition of the flag android:excludeFromRecents (which i know you have already seen):

Whether or not the task initiated by this activity should be excluded from the list of recently used applications ("recent apps"). That is, when this activity is the root activity of a new task, this attribute determines whether the task should not appear in the list of recent apps. "true" if the task should be excluded from the list; "false" if it should be included. The default value is "false".

so to remove the app from the list of recent app you can set this flag on the first activity in your application since that activity launches the the task for you application. If you have multiple tasks (unlikely for most apps) in your application then you need o set this flag for root activity of all the task.

Concinnous answered 14/11, 2012 at 18:54 Comment(1)
But what if I want to exclude the app from recents after launching the Activity?Olympie
R
7

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.

finishAndRemoveTask();
Reyesreykjavik answered 8/3, 2018 at 9:41 Comment(1)
Thank you. This was very helpful for removing an Instant App via a BackPress or SwipeLeft while leaving it in Recents otherwise.Bain
O
2

After receiving the other answers, I managed to get to the following workaround: I have an Activity, let's call it XPTO, declared in manifest with

and basically, when I want app to disappear from the recents list, I finish all other activities, then start the XPTO, which basically kills the app (calling android.os.Process.killProcess(android.os.Process.myPid()); in its onResume()

If anyone has a better way of doing this, please share it

Olympie answered 12/6, 2013 at 8:19 Comment(0)
A
0

For my needs by user action I had to stop all application process from the foreground service. When I had used the setExcludeFromRecents the app just disapeared from recent app forever - I didn't want this result, so I used finishAndRemoveTask. There is an example of my code:

applicationContext.getSystemService(ActivityManager::class.java).appTasks.firstOrNull { task ->
        task.taskInfo.baseActivity?.packageName == applicationContext.packageName
    }?.finishAndRemoveTask()
Anaerobic answered 22/9, 2023 at 7:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.