1

I have a WidgetResultActivity and a NotificationResultActivity, I set both their launchmode=singleInstance. But they have different behaviors: WidgetResultActivity will not opened from RECENT, while NotificationResultActivity will always be opened from RECENT(It is opened alone! Without MainActivity). So How can I forbid NotificationResultActivity opening from REcent, thx.

    <activity
        android:name=".WidgetResultActivity"
        android:launchMode="singleInstance"
        android:theme="@android:style/Theme.Translucent.NoTitleBar" />

    <activity
        android:name=".NotificationResultActivity"
        android:launchMode="singleInstance"
        android:theme="@android:style/Theme.Translucent.NoTitleBar" />

EDIT: Just See my ATester App, when I opened from notification...

Test code:

    NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(R.drawable.ic_launcher, getString(R.string.app_name), java.lang.System.currentTimeMillis());
    notification.flags = Notification.FLAG_NO_CLEAR;
    Intent intent = new Intent(this, ExcludeFromRecentActivity.class);
    // intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent activity = PendingIntent.getActivity(this, R.string.app_name, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    notification.setLatestEventInfo(this, "Open Activity: " + ABox.class.getSimpleName(), "new Intent()", activity);
    manager.notify(R.string.app_name, notification);

Test manifest:

    <activity
        android:name=".ATester1"
        android:label="@string/app_name" >
    </activity>
    <activity
        android:name=".ATester2"
        android:label="@string/app_name" >
    </activity>
    <activity
        android:name=".ATester3"
        android:label="@string/app_name" >
    </activity>
    <activity
        android:name=".ExcludeFromRecentActivity"
        android:excludeFromRecents="true"
        android:label="@string/app_name" >
    </activity>

Test Images:

1st:enter image description here

2nd:enter image description here

3rd:enter image description here

4th:enter image description here

EDIT: What I really need is DIALOG BEHEVIOR LIKE ACTIVITY, I will ask in another post. @WebnetMobile.com, thank you all the same.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
thecr0w
  • 2,148
  • 4
  • 33
  • 59

2 Answers2

2

To prevent activity from being added to recents, add

android:excludeFromRecents="true"

to its <activity> declaration in Manifest:

<activity
    android:name=".NotificationResultActivity"
    android:launchMode="singleInstance"
    android:excludeFromRecents="true"
    android:theme="@android:style/Theme.Translucent.NoTitleBar" />

See: android:excludeFromRecents in android docs.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
  • Thanks for quick reply. I tried, but "excludeFromRecents" will EXCULDE ALL MY ACTIVITY FROM RECENT.Right? – thecr0w Oct 23 '12 at 08:48
  • Not sure what is "all of activity"? There's activity or there is not. There's no half of activity :) The tag will prevent `NotificationResultActivity` to appear in recents. It will not affect other activityies - these will appear unless you add this attribute to their `` blocks too. – Marcin Orlowski Oct 23 '12 at 08:53
  • It DID affect other activityies: after NotificationResultActivity opened, my app disappeared in RECENTS. (User will not open my app from RECENT conveniently, hope you could got waht I mean) – thecr0w Oct 23 '12 at 09:01
  • There's no other way to affect recents than this tag. See http://developer.android.com/guide/topics/manifest/activity-element.html#exclude: _"Whether or not the task initiated by this activity should be excluded from the list of recently used applications ("recent apps"). That is, when this activity is the root activity of a new task, this attribute determines whether the task should not appear in the list of recent apps. "true" if the task should be excluded from the list; "false" if it should be included. The default value is "false"."_ – Marcin Orlowski Oct 23 '12 at 09:13
  • See above, My ATester disappears. – thecr0w Oct 23 '12 at 09:39
  • Please see this part: "Whether or not the **task** initiated by this activity should be excluded from the list of recently used applications". You should re-appear on recents once go to other activity of your app than NotificationResultActivity – Marcin Orlowski Oct 23 '12 at 09:45
  • Yes, you are right. But as u can see, ExcludeFromRecentActivity CLEAR my app from RECENTS, right? – thecr0w Oct 23 '12 at 09:52
  • But it cannot do differently. It always shows the last/current activity of your app. Since that one is disabled from recents it cannot show anything else, like "link" to previous activity, because it is not the place user is currently at. Prevent activities as I told and accept the way it works (you like it or not). – Marcin Orlowski Oct 23 '12 at 09:57
  • Think about dialog, DIALOG LIKE ACTIVITY is just what I want. – thecr0w Oct 23 '12 at 10:01
  • It is still an activity, just themed differently, so you will still have to exclude it as above. – Marcin Orlowski Oct 23 '12 at 10:14
  • What if I use window instead of activity, I guess dialog is a window as well. – thecr0w Oct 23 '12 at 10:21
  • if you use i.e. popup window then it will not be added to the recents (the underlying Activity will stay) so it may be a solution for you. – Marcin Orlowski Oct 23 '12 at 10:22
  • Still, your question was "how to prevent activity from recents" :) – Marcin Orlowski Oct 23 '12 at 10:51
1
android:taskAffinity="" 
android:excludeFromRecents="true" 
android:launchMode="singleInstance"

taskAffinity is needed, this works for me.

thecr0w
  • 2,148
  • 4
  • 33
  • 59
  • This happens because AndroidManifest attribute android:taskAffinity takes precedence over FLAG_ACTIVITY_NEW_TASK. If you want ActivityB to be in a different task than ActivityA you need to give it a different taskAffinity. – StepanM Jan 31 '18 at 16:08