开发者

How to remove application from recent application list?

开发者 https://www.devze.com 2023-01-17 13:21 出处:网络
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 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. 开发者_如何学CI 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?


try

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

in your AndroidManifest.xml's activity declaration.


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


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


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.


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"


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.


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.


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


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

精彩评论

暂无评论...
验证码 换一张
取 消