beginner
beginner

Reputation: 389

Activity does not show in the recents android

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.

Upvotes: 1

Views: 1047

Answers (3)

Priyank Patel
Priyank Patel

Reputation: 12382

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" />

Upvotes: 3

Swapnil
Swapnil

Reputation: 2502

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" />

Upvotes: 0

Jitesh Mohite
Jitesh Mohite

Reputation: 34180

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

Upvotes: 1

Related Questions