Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Starting the app from Notification remove it from recent app history

I have a problem in one of my apps when I start it from a notification. It never appears in the "recent app" list.

Without notifications, every thing works as expected: I start the app, navigate into and when I quit it (with home button or back button) after that I can go back into it with a long press on home button => ok.

The problem starts when I receive a notification. If I start the app from the notification, it starts the correct screen and I can use the app => ok. But when I quit the app (with home or back button) it do not appears anymore in the "recent app" list. More precisely:

  • If the app was present in the "recent app" list before launching the app from the notification, it removes it
  • If the app was not present in the "recent app" list, it does not add it

Below, my original code to add the notification in the status bar:

mNotification = new Notification(R.drawable.ic_notif, message, System.currentTimeMillis());
mNotification.flags |= Notification.FLAG_AUTO_CANCEL;

Intent notificationIntent = new Intent(Intent.ACTION_VIEW,Uri.parse(link));
notificationIntent.putExtra(...);

PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
mNotification.setLatestEventInfo(context, context.getString(R.string.app_name), message, contentIntent);
notificationManager.notify(ConfigApp.NOTIFICATION_ID, mNotification);

I tried adding a FLAG_ACTIVITY_NEW_TASK but it did not help:

Intent notificationIntent = new Intent(Intent.ACTION_VIEW,Uri.parse(link));
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
notificationIntent.putExtra(...);

Manifest declaration of the Activity started by the notification:

<activity
    android:name=".activity.ActivityMessages"
    android:label=""
    android:windowSoftInputMode="stateHidden">
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <data
            android:host="hostapp"
            android:pathPrefix="/prefixapp"
            android:scheme="schemeapp" />
    </intent-filter>
</activity>

Does anyone knows how to keep the app in the "recent app" list after being started form a notification?

like image 861
ol_v_er Avatar asked Aug 31 '12 08:08

ol_v_er


1 Answers

Add the action android.intent.action.MAIN and the category android.intent.category.LAUNCHER to the specification of your activity. Having two actions and categories seem like a hack but it's actually supported by Android.

<activity
    android:name=".activity.ActivityMessages"
    android:label=""
    android:windowSoftInputMode="stateHidden">
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
        <data
            android:host="hostapp"
            android:pathPrefix="/prefixapp"
            android:scheme="schemeapp" />
    </intent-filter>
</activity>

That should add it to the recent applications list. The reason is described in the Android developer documentation:

An intent filter of this kind causes an icon and label for the activity to be displayed in the application launcher, giving users a way to launch the activity and to return to the task that it creates any time after it has been launched.

This second ability is important: Users must be able to leave a task and then come back to it later using this activity launcher. For this reason, the two launch modes that mark activities as always initiating a task, "singleTask" and ""singleInstance", should be used only when the activity has an ACTION_MAIN and a CATEGORY_LAUNCHER filter. Imagine, for example, what could happen if the filter is missing: An intent launches a "singleTask" activity, initiating a new task, and the user spends some time working in that task. The user then presses the Home button. The task is now sent to the background and is not visible. Now the user has no way to return to the task, because it is not represented in the application launcher.

like image 120
Nicholas Avatar answered Oct 18 '22 22:10

Nicholas