Starting the app from Notification remove it from

2020-06-08 15:21发布

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?

5条回答
祖国的老花朵
2楼-- · 2020-06-08 15:30

Old topic but I discover solution (and I can't find in web anyone), maybe it will be helpful for sb. It doesn't give possibility to set any activity as a parent but I think (not sure) that you can change it programmatically (in OpenFromNotificationActivity) if it is necessary.

In manifest

<activity android:name=".OpenFromNotificationActivity" 
            android:label="@string/label">
            <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".StartSplashActivity" />
 </activity>

 <activity android:name=".StartSplashActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
 </activity>
查看更多
我只想做你的唯一
3楼-- · 2020-06-08 15:32

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.

查看更多
Juvenile、少年°
4楼-- · 2020-06-08 15:39

Try adding to the manifest the following:

android:excludeFromRecents = "false"

Good luck, Luis

查看更多
我只想做你的唯一
5楼-- · 2020-06-08 15:45

I bumped into same issue. The problem was that my activity had android:label="". When I open Activity from the Notification then it replaces root activity in the app task with current one. And Android excludes activities with empty label from Recents (see source code).

To hide title of activity you should use:

getActionBar().setDisplayShowTitleEnabled(false);
查看更多
家丑人穷心不美
6楼-- · 2020-06-08 15:48

You need to use separate Intent Filters for each action/category pair:

<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>
    <intent-filter>
        <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>
查看更多
登录 后发表回答