5

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

Sankar
  • 1,685
  • 2
  • 16
  • 27

2 Answers2

5

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.

Divers
  • 9,531
  • 7
  • 45
  • 88
  • Thanks, It's working. I did try adding taskAffinity to MainActivity but I think here both SingleTask and taskAffinity combined doing the trick. But in developer docs it's mentioned that "The first activity should include an attribute setting of android:taskAffinity="" in the app's AndroidManifest.xml file, and the intent within the static shortcut should point at this first activity." If we look at this point taskAffinity should be there for the TempActivity, isn't it? – Sankar Mar 07 '17 at 12:55
0

Presumable you are using FLAG_ACTIVITY_NEW_TASK because you want a clean, new version of the activity when launched from this shortcut.

You can switch MainActivity launch mode to singleInstance. Then you have the option of using onStart/onRestart or onNewIntent(Intent) to clear down (and reset the activity and the TempActivity hack will no longer be required).

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    if (intent.getAction().equals("custom")) {
        //reset
    }
}
Nick Cardoso
  • 20,807
  • 14
  • 73
  • 124