Android Shortcut start causes other Activity to be destroyed

question

We all know that starting from API 25, Android has added a function similar to 3D Touch, that is, long press the desktop icon to pop up a shortcut menu (up to 4). The desktop Launcher of the early domestic system did not actively adapt to this function, so the major applications were too lazy to do it, and it was gradually improved later. At present, including WeChat, Alipay, etc., you can long press and pop up shortcuts, and Alipay supports dynamic configuration.
Please add a picture description
Just look at the official development documentation: https://developer.android.com/guide/topics/ui/shortcuts , static shortcut adaptation is very simple, just add the xml file and you’re done, so I won’t go into details here.

However, in the actual experience development process, it is found that after opening the corresponding page of the application through the shortcut, other activities will be destroyed. This is not the effect we want.

Simple Analysis

This phenomenon is very similar to setting the CLEAR_TASK flag when starting the Activity, causing the task stack to be cleared. However, judging from the following usage examples, the static shortcut cannot set the flag of the Intent, and the relevant logic is implemented internally by the system SDK.

<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
  <shortcut
    android:shortcutId="compose"
    android:enabled="true"
    android:icon="@drawable/compose_icon"
    android:shortcutShortLabel="@string/compose_shortcut_short_label1"
    android:shortcutLongLabel="@string/compose_shortcut_long_label1"
    android:shortcutDisabledMessage="@string/compose_disabled_message1">
    <!-- 例1 -->
    <intent
      android:action="android.intent.action.VIEW"
      android:targetPackage="com.example.myapplication"
      android:targetClass="com.example.myapplication.ComposeActivity" />
    <!-- 例2 -->
    <intent
      android:action="android.intent.action.VIEW"
      android:data="xxx://xxx/xxx" />
  </shortcut>
  <!-- Specify more shortcuts here. -->
</shortcuts>

Later, I saw such a paragraph in the official document:

Static shortcuts cannot have custom intent tags. The first intent of a static shortcut always has Intent.FLAG_ACTIVITY_NEW_TASK and Intent.FLAG_ACTIVITY_CLEAR_TASK set. This means that if the app is already running, all existing activities in the app will be destroyed when the static shortcut starts. If you don't want this behavior you can use Trampoline Activity...

solve

This Trampoline means to set up a springboard Activity to distribute and start the target Activity, and we need to make this springboard Activity and other activities of the application not in the same stack. It is very simple, just set the property taskAffinity:

<!-- AndroidManifest.xml -->
<activity
  android:name=".TrampolineActivity"
  android:taskAffinity="" />
  
<!-- xml/shortcuts.xml -->
<intent
  android:action="android.intent.action.VIEW"
  android:targetPackage="com.example.myapplication"
  android:targetClass="com.example.myapplication.TrampolineActivity" />

The setting of taskAffinity is not displayed, and its default value is the package name, so just set a string other than the package name for our springboard Activity. In this way, when the application is opened by long pressing the shortcut on the desktop, other pages will not be destroyed.

Guess you like

Origin blog.csdn.net/ysy950803/article/details/120512352