I have an Activity (MainActivity) in my application and there is one static shortcut (pointing to TempActivity).
Since for static labels FLAG_ACTIVITY_NEW_TASK and FLAG_ACTIVITY_CLEAR_TASK will always be set, I created TempActivity, which is an invisible action, i.e. It launches MainActivity and then calls finish (). Also, as suggested in the documentation for developers, SecondActivity has android: taskAffinity = "" in the AndroidManifest.xml application file.
MainActivity has android: launchMode = "singleTop"
Even after this, MainActivity is launched in a new task instead of the existing one (created at startup from the main screen).
AndroidManifest.xml
<activity
android:name=".MainActivity"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.app.shortcuts"
android:resource="@xml/shortcuts" />
</activity>
<activity android:name=".TempActivity" android:taskAffinity=""></activity>
<shortcut
android:enabled="true"
android:icon="@drawable/icon"
android:shortcutDisabledMessage="@string/app_name"
android:shortcutId="static"
android:shortcutLongLabel="@string/app_name"
android:shortcutShortLabel="@string/app_name">
<intent
android:action="custom"
android:targetClass="com.example.mobile.appshortcut.TempActivity"
android:targetPackage="com.example.mobile.appshortcut" />
</shortcut>
TempActivity.java
public class TempActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = new Intent();
intent.setClass(this,MainActivity.class);
startActivity(intent);
finish();
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
}
}
https://developer.android.com/reference/android/content/pm/ShortcutManager.html