Wednesday, March 21, 2018

Exclude current activity from Recent Tasks

Leave a Comment

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

4 Answers

Answers 1

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.

Answers 2

you are defined it in manifest that is enough but it is not coming..

ok..try add this flag also to your Intnet and start the Activity..

intnet.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); 

Answers 3

Should the user be able to return to it? (Meaning if it shows and user clicks recents, have they lost their chance to see the hidden activity or should it still be there?)

If they cannot return to it anyway then the best action would be to finish() the activity onPause(). This will mean that as long as you have used android:excludeFromRecents and android:noHistory="true", there will be no item in recents.

If however you do wish to return to the 'interruption' activity (but do not want a recents entry) you could consider still finishing the activity onPause - but also recording a preference flag (something like IS_INTERSTITIAL). Your other activities can then check this preference in onResume and, if it is true, send an Intent to restart the Interstitial instead - To the user it will just appear they are resuming the app in the same state as they left it (interstitial)

Edit: If the screen needs to stay (rather than be re-instantiated) it may be possible to use a DialogFragment, although then you must worry about configuration changes. There is one hack you could try (explained in this answer) -

Set your label empty By using android:label="" your recent task is excluded. however this is a definite hack which may produce inconsistent results (as I haven't tested it to be sure)

Answers 4

Use the below code and then your MainActivity will not appear in Recent Tasks.

Intent i = new Intent(context, MainActivity.class); i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); i.setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); i.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK); context.startActivity(i); 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment