0

I would like to exclude my app from the recent apps list (long press home, multitasking button, etc) BUT only for Android 4.4.1 and 4.4.2. The reason being is that on these Android versions, services can't restart themselves and my service is killed when my app is swiped away from the recents list. Normally it restarts itself without a problem, but it can't do so on 4.4.1 and 4.4.2.

I know you can add android:excludeFromRecents="true" in the manifest but that will exclude it on all Android versions.

I tried what was suggested here but can't get it to work:

Remove app from recent apps programmatically

Any ideas?

Community
  • 1
  • 1
Flyview
  • 1,899
  • 1
  • 28
  • 46

2 Answers2

1

To exclude yourself from the recent-tasks list on 4.4 (not only 4.4.2):

Step #1: Create res/values/bools.xml with a bool resource, named is_44, set to false

Step #2: Create res/values-v19/bools.xml with a bool resource, named is_44, set to true

Step #3: Create res/values-v21/bools.xml with a bool, named is_44, set to false

Step #4: Use android:excludeFromRecents="@bool/is_44" in your <activity>

So, for API Level 19 and 20, you will be excluded from recents, but not other API levels.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Although this does not specifically target 4.4.2 it is good enough for me, so I will be selecting it as the correct answer. I guess there is no way to do this programmatically once we know the exact build version? Also, I think you mixed up the bool values, wouldn't you want it to be true for v19? – Flyview Mar 04 '15 at 23:52
  • @Flyview: "I guess there is no way to do this programmatically once we know the exact build version?" -- I don't think so. "Also, I think you mixed up the bool values, wouldn't you want it to be true for v19?" -- no, I think I have it right. `is_not_44` should be `false` for 19 and 20 (Android 4.4/4.4W) and `true` for everything else. – CommonsWare Mar 05 '15 at 00:00
  • But then android:excludeFromRecents becomes false for v19 and v20! Don't we want this true so that the app is excluded from the recent apps list? – Flyview Mar 05 '15 at 00:04
  • Also I noticed that while this approach does work to remove the app from recents list for 4.4.x, the app still shows up in the recents list if accessing opening the list with the activity still in the foreground. This still allows the user to swipe it away and thereby kill the service forever. – Flyview Mar 05 '15 at 00:11
  • @Flyview: ::sigh:: I think I got confused by a triple-negative. `is_44` will be `true` for 19 and 20 and therefore you will be excluded from recents for those. "the app still shows up in the recents list if accessing opening the list with the activity still in the foreground" -- I am not aware of any way of affecting that behavior. – CommonsWare Mar 05 '15 at 00:13
  • Perfect, I had already used is_44. Thanks for editing your answer! You're something of a stackoverflow celebrity. :) – Flyview Mar 05 '15 at 00:24
1

If Android 4.4.x is exact enough you could create a folder layout-v19 and define an extra layout for the activity, which should be excluded from the recent apps list:

<activity 
    android:name=".anActivity"
    android:excludeFromRecents="true"
    ...>

See Providing Resources documentation (search for Platform Version) and API levels here

Trinimon
  • 13,839
  • 9
  • 44
  • 60