Activity excludeFromRecents not working on Android 5.0

人走茶凉 提交于 2020-01-12 18:50:27

问题


I'm trying to finish an activity and not have it on the recents. The following code seems to work on KitKat but not on lolipop, as the activity always shows on the recents.

intentInvite = new Intent( context, OnInviteActivity.class );
intentInvite.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intentInvite.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intentInvite = createInviteIntent( intentCloud, intentInvite );
context.startActivity( intentInvite );

AndroidManifest.xml

<activity android:name=".OnInviteActivity"
          android:label="@string/app_name"
          android:excludeFromRecents="true"
          android:noHistory="true"

回答1:


Try adding an unique taskAffinity:

<activity android:name=".OnInviteActivity"
          android:label="@string/app_name"
          android:taskAffinity=".OnInviteActivity"
          android:excludeFromRecents="true"
          android:noHistory="true"



回答2:


This is a known issue into Android 5.0, since L preview. Seems Google is working on it.

Below are open issues for the same

  1. android:excludeFromRecents not works
  2. excludeFromRecents="true" not working in Android L



回答3:


https://stackoverflow.com/a/27633500/1269737 - that's well known Android Issue.

This can be done programmatically

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

If Task Root Activity is excluded from recent, all activities in this task will be excluded too.




回答4:


I had the same issue with android Jelly Bean and I had this in my manifest:

<activity android:name=".MainActivity"
          android:label="@string/app_name"
          android:excludeFromRecents="true"
          android:noHistory="true" />

I removed android:noHistory="true" and it worked, the recent apps stopped showing my activity. Looks like the noHistory and execludeFromRecents are not compatible together for some reason.



来源:https://stackoverflow.com/questions/27633329/activity-excludefromrecents-not-working-on-android-5-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!