问题
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
- android:excludeFromRecents not works
- 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