Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Activity excludeFromRecents not working on Android 5.0

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"
like image 978
Jani Avatar asked Dec 24 '14 07:12

Jani


People also ask

What is excludeFromRecents Android?

android:excludeFromRecents ensures the task is not listed in the recent apps. That is the reason, when android:excludeFromRecents is set to true for FromNotificationActivity , MainActivity disappers from history. Solution: Use android:taskAffinity to specify different tasks for both the activities.

What is the use of activity tag?

You can use the manifest's <activity> tag to control which apps can start a particular activity. A parent activity cannot launch a child activity unless both activities have the same permissions in their manifest.


2 Answers

Try adding an unique taskAffinity:

<activity android:name=".OnInviteActivity"
          android:label="@string/app_name"
          android:taskAffinity=".OnInviteActivity"
          android:excludeFromRecents="true"
          android:noHistory="true"
like image 133
Damien Oliver Avatar answered Oct 26 '22 22:10

Damien Oliver


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.

like image 22
StepanM Avatar answered Oct 26 '22 23:10

StepanM