user412759
user412759

Reputation: 2415

How to remove application from recent application list?

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?

Upvotes: 61

Views: 53681

Answers (9)

jackylalala
jackylalala

Reputation: 141

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.

Upvotes: 14

Tushar Saha
Tushar Saha

Reputation: 2106

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()
        }
    }
}

Upvotes: 1

Mahmouf Mrad
Mahmouf Mrad

Reputation: 21

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);

Upvotes: 1

user632905
user632905

Reputation: 1000

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

Upvotes: 17

whlk
whlk

Reputation: 15635

try

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

in your AndroidManifest.xml's activity declaration.

Upvotes: 211

Kislingk
Kislingk

Reputation: 1457

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">

Upvotes: 21

MastaCarlos
MastaCarlos

Reputation: 31

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.

Upvotes: 2

Cheryl Simon
Cheryl Simon

Reputation: 46844

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.

Upvotes: 3

Macarse
Macarse

Reputation: 93143

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"

Upvotes: 4

Related Questions