1

Scenario - I dont have my application running in the background. I have a receiver implemented for ACTION_USER_PRESENT. In this receiver i start a activity whose manifest settings are :

<activity android:name=".activity.MyActivityB"
        android:excludeFromRecents="true"
        android:noHistory="true"
        android:screenOrientation="portrait"/>

This is because there is banner in it for which i credit points to the user and i dont want the user to be able to launch it again and again for the same banner .

From this activity i launch another activity on some button press. This new Activity MyNewActivity is launched with this :

 Intent intent = new Intent(MyActivityB.this, MyNewActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                this.startActivity(intent);
                Intent closeintent = new Intent("MAIN_ACTIVITY_CLOSE");
                closeintent.putExtra("action", "close");
              LocalBroadcastManager.getInstance(this).sendBroadcast(closeintent);
                MyActivityB.this.finish();

Now my new activity should appear in the recents if I long press home button or the recents button .

But the problem is , when i press home MyNewActivity is not being shown in the recents.

The MyNewActivity is defined in Manifest like this :

 <activity
        android:name=".activity.MyNewActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait" />

what am i doing wrong and why not MyNewActivity is appearing in the recents.

beginner
  • 383
  • 3
  • 5
  • 19

3 Answers3

3

As per document android:excludeFromRecents

Task initiated by this activity should be excluded from the list of recently used applications, the overview screen. 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.

By default, all the activities of an application have the same affinity. Activities with same affinity conceptually belong to the same task. Hence in this case both MyActivityB and MyNewActivity belong to the same task. android:excludeFromRecents ensures the task is not listed in the recent apps.

That is the reason, when android:excludeFromRecents is set to true for MyActivityB, MyNewActivity disappers from history.

Solution:

Use android:taskAffinity to specify different tasks for both the activities. Use android:excludeFromRecents for MyActivityB if that task should not be shown in history at all.

<activity android:name=".activity.MyActivityB"
        android:taskAffinity=".MyActivityB"
        android:excludeFromRecents="true"
        android:noHistory="true"
        android:screenOrientation="portrait"/>

 <activity
        android:name=".activity.MyNewActivity"
        android:taskAffinity=".MyNewActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait" />
Priyank Patel
  • 12,244
  • 8
  • 65
  • 85
  • If i give task affinity to both of them then the MyNewActivity is launched and shown in recents but if my app was then in the background with main activity then there are two instances of my app is shown in the recents – beginner Nov 24 '16 at 09:53
  • Give same taskaffinity value to all activities(".MainActivity") and for MyActivityB give different value(".MyActivityB") – Swapnil Nov 24 '16 at 10:02
  • 1
    Try with removing `android:taskAffinity=".MyNewActivity"` from `MyNewActivity` – Priyank Patel Nov 24 '16 at 10:04
  • After lot of efforts , what i found is android:excludeFromRecents="true" removing this and starting the new activity like this : intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); I read somewhere about this that it will remove the current task and makes the new task the start of the new task stack. But then the activity B on home press is still shown in the recents . – beginner Nov 24 '16 at 11:34
1

remove android:excludeFromRecents="true" from your menifest. because it will remove your app from recents.

Jitesh Mohite
  • 31,138
  • 12
  • 157
  • 147
0

Whether or not the task initiated by this activity should be excluded from the list of recently used applications, the overview screen. 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. Set "true" if the task should be excluded from the list; set "false" if it should be included. The default value is "false".

From this android:excludeFromRecents removes all the activities in the current task. In android all the activities in application have same affinity and belongs to same Task unless you specify.In your case as new activity is starting in same task as your previouse activity and android System is removing new Activity as well from the recent list. So you need to specify android:taskAffinity in your manifest for each Activity in manifest and it will fix your issue.

Please refer android documentation for more details.

Fix: Use android:taskAffinity

<activity android:name=".activity.MyActivityB"
        android:excludeFromRecents="true"
    android:taskAffinity=".MyActivityB"
        android:noHistory="true"
        android:screenOrientation="portrait"/>

 <activity
        android:name=".activity.MyNewActivity"
    android:taskAffinity=".MyNewActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait" />
Swapnil
  • 2,409
  • 4
  • 26
  • 48
  • If i give task affinity to both of them then the MyNewActivity is launched and shown in recents but if my app was then in the background with main activity then there are two instances of my app is shown in the recents – beginner Nov 24 '16 at 09:53
  • Give same taskaffinity value to all activities and for MyActivityB give different value – Swapnil Nov 24 '16 at 10:01
  • If I dont assign to any other activity and only to MyActivityB then also it means that others have the same affinity (default ). – beginner Nov 24 '16 at 10:19
  • yes by default all the activities have same affinity. – Swapnil Nov 24 '16 at 10:33