How to stop Android 7.1 app shortcuts from ignoring Activity launchMode?

I have a static app shortcut declared like this:

<shortcut
    android:enabled="true"
    android:icon="@drawable/shortcut"
    android:shortcutDisabledMessage="@string/downloads"
    android:shortcutId="downloads"
    android:shortcutLongLabel="@string/downloads"
    android:shortcutShortLabel="@string/downloads">

    <intent
        android:action="android.intent.action.VIEW"
        android:targetClass="com.colinrtwhite.test.activity.DownloadsActivity"
        android:targetPackage="com.colinrtwhite.test"/>
</shortcut>

      

and it is declared in my AndroidManifest.xml like this:

<activity
    android:name=".activity.DownloadsActivity"
    android:launchMode="singleTask"
    android:theme="@style/AppTheme"/>

      

According to the documentation , startup mode singleTask

should reuse existing instances of Activity and pass new intents through the onNewIntent method. However, if I have an existing DownloadsActivity instance and click on the app shortcut to launch it, it will destroy and then recreate the Activity.

My question is, how do I force the app shortcut to reuse an existing instance of my activity and not restart it?

+3


source to share


1 answer


You are using a static shortcut, according to the documentation

Static labels cannot have custom intent flags. The first intent of a static shortcut will always have FLAG_ACTIVITY_NEW_TASK and FLAG_ACTIVITY_CLEAR_TASK set. This means that when the application is already running, all existing activities will be destroyed when the static shortcut is launched.

Also according to the same part



Dynamic shortcuts can be published with any set of Intent flags. Typically, FLAG_ACTIVITY_CLEAR_TASK is specified, possibly along with other flags;

https://developer.android.com/reference/android/content/pm/ShortcutManager.html , part of Shortcut Intents.

+4


source







All Articles