2

I'm trying to remove my app from recents (the app list you get when you press the "square" button, just to avoid misunderstandings). I found the piece of code below to do that and it's working well. The problem is the app has minimum SDK = 19, so it won't work on older devices. I can't find anyway to do that around the internet. does anybody know a way?

EDIT: I'm aware that I can do that using a new activity and closing it immediately but I would like to avoid that

EDIT2: this must be done programatically as it depends on user's preferences

//remove the app from recents. TODO-background doesn't remove if app list button is pressed instead of home
if(android.os.Build.VERSION.SDK_INT >= 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);
        }
    }
} else {
    //TODO-background need to find a way for API 19 and 20
}

PS if someone knows a way to also solve the first TODO issue, it'd welcome (I'll eventually open another question)

jack_the_beast
  • 1,838
  • 4
  • 34
  • 67

2 Answers2

0
String nameOfProcess = "com.accurate.compass.directionfinder.map";
    ActivityManager  manager = (ActivityManager)getActivity().getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningAppProcessInfo> listOfProcesses = manager.getRunningAppProcesses();
    for (ActivityManager.RunningAppProcessInfo process : listOfProcesses)
    {
        if (process.processName.contains(nameOfProcess))
        {
            // Ends the app
            manager.restartPackage(process.processName);
            break;
        }
    }
Axbor Axrorov
  • 2,720
  • 2
  • 17
  • 35
S.J Hashmi
  • 73
  • 9
-1

If you just want it not to be shown in recents, you should use a simple one line code : exclude from recents


From your manifest file search where is your Application activity tag is :

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/my_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity" 
              android:excludeFromRecents= "true" >

Note in above code android:excludeFromRecents= "true" is just what you want if you do not want your app to be shown in recents when closed.

It works from API 11 to API 27. Hope it helps

  • Why you can not do that, please elaborate more so that i can or someone else can throw the light on it. –  Apr 26 '18 at 15:09
  • 1
    if I do that or not depends on user's preferences, so it can change anytime. I've updated my question – jack_the_beast Apr 26 '18 at 15:11