Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exclude current activity from Recent Tasks

Tags:

My app, running in background, at some point displays activity to purposefully interrupt user's flow. My activity starts a new task, which appears in "Recent Tasks" lists while being in foreground. Is there any way it could be prevented? Setting android:excludeFromRecents does not work - activity is not presented anymore in "Recent Tasks" only after is has been paused.

Manifest looks like:

<activity
    android:name="com.example.recenttasks.MainActivity"
    android:excludeFromRecents="true">
</activity>

and activity is started this way:

@Override
public void onReceive(Context context, Intent intent) {
    Intent i = new Intent(context, MainActivity.class);
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(i);
}
like image 774
lstipakov Avatar asked Dec 11 '13 10:12

lstipakov


People also ask

Can activity have only one task?

The activity can only be running as the root activity of the task, the first activity that created the task, and therefore there will only be one instance of this activity in a task.

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 flag_ activity_ new_ task?

When using this flag, if a task is already running for the activity you are now starting, then a new activity will not be started; instead, the current task will simply be brought to the front of the screen with the state it was last in. See FLAG_ACTIVITY_MULTIPLE_TASK for a flag to disable this behavior.

What is Android launchMode singleTop?

Single Top You can use this launch mode to generate numerous instances of the same activity within the same task or across tasks, but only if the identical instance does not already exist at the top of the stack. Syntax: <activity android:launchMode=”singleTop” />


1 Answers

The key thing you mentioned is

appears in "Recent Tasks" lists while being in foreground

I think you can't change that behavior. I just tested on my app and the same thing happens. If I press the "recent tasks" button while having my activity in the foreground, it appears listed there. The moment I move out of it to another activity or to my phone's main screen, the activity is not listed anymore.

I also tested this on the built-in DeskClock app that comes with recent Android versions and the same behavior is present there when a new alarm is triggered. Note that the AlarmAlertFullscreen activity of that app has the same parameters you mentioned in your question.

I'm not sure you can circumvent this or why you would need to in the first place since the activity is not listed anymore once it loses the focus.

like image 78
Ricardo Avatar answered Sep 28 '22 11:09

Ricardo