活动excludeFromRecents在Android 5.0上无效

Jan*_*ani 7 android android-fragments android-activity

我正在尝试完成一项活动而不是最近的活动.以下代码似乎适用于KitKat,但不适用于lolipop,因为活动始终显示在最近.

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 );
Run Code Online (Sandbox Code Playgroud)

AndroidManifest.xml中

<activity android:name=".OnInviteActivity"
          android:label="@string/app_name"
          android:excludeFromRecents="true"
          android:noHistory="true"
Run Code Online (Sandbox Code Playgroud)

小智 6

尝试添加一个独特的任务亲和力:

<activity android:name=".OnInviteActivity"
          android:label="@string/app_name"
          android:taskAffinity=".OnInviteActivity"
          android:excludeFromRecents="true"
          android:noHistory="true"
Run Code Online (Sandbox Code Playgroud)


use*_*737 5

/sf/answers/1934345031/ - 这是众所周知的 Android 问题。

这可以通过编程方式完成

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

如果从最近排除任务根活动,则该任务中的所有活动也将被排除。