Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to launch existing instance of an activity from static shortcut

Tags:

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

like image 332
Sankar Avatar asked Mar 03 '17 15:03

Sankar


1 Answers

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.

like image 91
Divers Avatar answered Sep 22 '22 10:09

Divers