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)