How to remove application from recent application list?
Asked Answered
S

9

61

I guess that Android won't let people to do this, because they think they have perfect handle for the task/applications. However, I really need to do this in my case.

I have an activity A acting as the entry point of my application. In that activity, it reads the preference and decided which activity to start, say B or C. After that, it finishes itself. So, activity A never appears to the users.

My application stores things on sdcard, and reads from it constantly. So, when the sdcard is unmounted, I need to display a message to the user that the sdcard is unavailable, instead of opening B or C. I set a check in A to display that message when sdcard is unavilable. When that message is displayed, A will not try to start B or C.

Things works perfectly if user only enter my application from application launcher. However, I found that user can also enter my application by long pressing home and choose it from the recent application list, if he has opened it recently. When user does that, it skips A and goes directly to B or C. I don't have the check in both of them, so exception is thrown while I am trying to access sdcard, and force close dialog pops up.

I can simply move my check to both B and C to fix this problem. But in the future, the number of activities started from A will increase. If there are 6 of them, I'll need to copy this check to 6 places. Needless to say, this looks very ugly, and is a maintenance nightmare.

So, the best fix should be removing my application from recent application list when the sdcard is uunmounted. However, I can't find how to do this. Even killing the process or use ActivityManager.restartPackage, it still appears in the list. Can anyone tell me how to remove it from the list?

Shizukoshizuoka answered 21/9, 2010 at 17:21 Comment(2)
I really need to do this in my case Then your design is wrongViceroy
this answer https://mcmap.net/q/321410/-how-to-remove-application-from-recent-application-list not worked for me @Shizukoshizuoka did u tried any extra solutionsWordplay
M
211

try

<activity android:name=".MainActivity"
        android:excludeFromRecents="true" ...

in your AndroidManifest.xml's activity declaration.

Maidenhead answered 24/6, 2011 at 14:9 Comment(1)
Activity shows in recent list if I hit overview (recent apps) buttonChloride
L
21

Other attributes can help your activity isolate from other activities in the same package.

<activity 
android:name=".aActivity"
android:excludeFromRecents="true"
android:taskAffinity=""
android:launchMode="singleInstance">
Lancewood answered 15/6, 2012 at 23:10 Comment(0)
L
17

just add android:excludeFromRecents="true" in your activity's tag in the manifest file..its easy

Logway answered 17/11, 2011 at 20:5 Comment(0)
L
14

You need to set below code in Launcher Activity in AndroidManifest.xml :

<activity>
  ...
  android:excludeFromRecents="true"
  ...
</activity>

or start this activity from

intent.setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);

and also in AndroidManifest.xml

<activity>
  ...
  android:label=""
  ...
</activity>

Setting label as empty will let this activity NOT be shown in Recent App list.

Lend answered 8/6, 2015 at 1:57 Comment(0)
V
4

I would not recommend to do so but I would try the following options:

Option 1:

On B and C add:

protected void onPause() {
  finish();
}

Option 2:

Add to B and C the following in the AndroidManifest:

android:noHistory= "true"
Veteran answered 21/9, 2010 at 17:33 Comment(0)
T
3

Removing your application from the recent apps list is probably not possible, and definitely not the best solution. That will just confuse the user who expects all apps to behave similarly.

Regardless, I don't think it will solve your problem. If the user hits home while on Activity B, then selects your app from the home page, it will start Activiy B again.

There are a number of ways to solve the real problem. One easy one might be to create a base Activity that performs the SD card check, and have all of your activities extend from it. That way the check is only in one place.

Tully answered 21/9, 2010 at 17:40 Comment(0)
H
2

OK, I know for a fact that it can be done in 2.3.4. The app Toddler Lock when opened clears the recent app list so that when you "Lock" your phone if you long press home key the list is blank. Unfortunately I have not found how to do it. So for anyone who is looking and reading postf that say it is not possible don't give up. I sure heck am not.

Holyhead answered 13/9, 2011 at 5:33 Comment(0)
F
1

if you wanna exit Application on Button click use this code :

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);

To kill the complete app and remove it from Runningapp list kill the app through its pid(its nasty)... use this lines before above code.

int pid = android.os.Process.myPid();
android.os.Process.killProcess(pid);
Featherhead answered 18/5, 2016 at 19:23 Comment(0)
C
1

try this, it will finish all activities and remove app from recents

private fun killCurrentApp() {
    val am = this.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager?
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        val appTasks = am!!.getAppTasks()
        if (appTasks.size > 0) {
            val appTask = appTasks.get(0)
            appTask.finishAndRemoveTask()
        }
    }
}
Complementary answered 23/10, 2019 at 11:19 Comment(1)
Works perfect! And it doesn't require minimum version of API 21Vannie

© 2022 - 2024 — McMap. All rights reserved.