I have an Activity(MainActivity) in my application and have one static shortcut(pointed to TempActivity).
As the static shortcuts will always have FLAG_ACTIVITY_NEW_TASK and FLAG_ACTIVITY_CLEAR_TASK set, I've created TempActivity which is an invisible Activity i.e It'll start MainActivity and then calls finish(). And also as suggested in developer docs SecondActivity has android:taskAffinity="" in the app's AndroidManifest.xml file.
MainActivity has android:launchMode="singleTop"
Even after doing this still MainActivity is getting launched in new task instead of existing task(created when launched from home 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
<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);
//setContentView(R.layout.activity_main2);
//Intent intent = getIntent(); // From Shortcut
Intent intent = new Intent(); // For Testing
intent.setClass(this,MainActivity.class);
startActivity(intent);
finish();
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
}
}
Link to developer docs https://developer.android.com/reference/android/content/pm/ShortcutManager.html
android:taskAffinity=""
should be on MainActivity
, not TempActivity
.
So your AndroidManifest should be something like that:
<activity
android:launchMode="singleTask"
android:taskAffinity=""
android:name=".MainActivity">
<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/shortcut"/>
</activity>
<activity
android:name=".TempActivity"/>
And TempActivity
public class TempActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startActivity(new Intent(this, MainActivity.class));
finish();
}
}
And just to make sure, publishing my shortcut.xml
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
<shortcut
android:enabled="true"
android:shortcutDisabledMessage="@string/app_name"
android:shortcutId="compose"
android:shortcutLongLabel="@string/app_name"
android:shortcutShortLabel="@string/app_name">
<intent
android:action="android.intent.action.VIEW"
android:targetClass="kidinov.org.test.TempActivity"
android:targetPackage="kidinov.org.test"/>
<categories android:name="android.shortcut.conversation"/>
</shortcut>
</shortcuts>
I've created sample project - it works fine. Feel free to check it out.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With