3

I have a static app shortcut declared like so:

<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, the singleTask launch mode is supposed to re-use existing instances of an Activity and pass new intents through the onNewIntent method. However, if I have an existing instance of DownloadsActivity and tap the app shortcut to launch it, it will destroy then recreate the Activity.

My question: how do I force the app shortcut to re-use an existing instance of my Activity and not restart it?

1 Answer 1

5

You are using static shortcut, according to documentation

Static shortcuts 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, when the app is already running, all the existing activities will be destroyed when a 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 Shortcut Intents.

1
  • Hmm so it looks like using a dynamic shortcut might fix my problem. I'll try it out later tonight and report back (and accept your answer if it fixes the issue). Thanks! Mar 21, 2017 at 23:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.