5

Sorry if this seems like a duplicated question as Deep link is causing multiple instances of the app to open . However, I think from the comments and answers, most of us can hardly understand the question without proper screenshots.

Please allow me to further elaborate the question, before you decide to mark this as duplicated.

There are 2 ways to launch my app

  1. By clicking on the app icon
  2. By clicking on a deep link

What I'm trying to achieve is that, regardless how I launch the app, whether through deep link or clicking on the app icon, I wish to have only 1 instance of the app appears in the task list.

But, that is not the case. Please check below on how do I reproduce the problem.


Step 1 : Click on the app icon

enter image description here

Step 2 : Check on Task list

enter image description here

Step 3 : Launch GMail app and check on task list

enter image description here

Step 4 : Click on the deep link in email, then check on task list again

enter image description here


As you can see in the task list. There are 2 instances of app. The front most is invoked using deep link, and back most is invoked by clicking on the app icon.

I try to apply the following techniques together, to ensure only 1 instance of app in task list.

singleTop

    <activity
        android:name=".JStockFragmentActivity"
        android:launchMode="singleTop"
        android:label="@string/title_activity_main" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

        <intent-filter android:autoVerify="true">
            <!-- Sets the intent action to view the activity -->
            <action android:name="android.intent.action.VIEW" />
            <!-- Allows the link to be opened from a web browser -->
            <category android:name="android.intent.category.BROWSABLE" />
            <!-- Allows the deep link to be used without specifying the app name -->
            <category android:name="android.intent.category.DEFAULT" />

            <!-- Accepts URIs that begin with "http://jstock.co/a/investing” -->
            <data android:scheme="http"
                android:host="jstock.co"
                android:pathPrefix="/a/investing" />

            <!-- Accepts URIs that begin with "https://jstock.co/a/investing” -->
            <data android:scheme="https"
                android:host="jstock.co"
                android:pathPrefix="/a/investing" />
        </intent-filter>
    </activity>

isTaskRoot

This technique is being mentioned in https://stackoverflow.com/a/7748416/72437

public class JStockFragmentActivity extends AppCompatActivity implements GoogleApiClientFragment.ConnectionCallbacks {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        Utils.updateTheme(this);

        super.onCreate(savedInstanceState);

        // http://stackoverflow.com/questions/4341600/how-to-prevent-multiple-instances-of-an-activity-when-it-is-launched-with-differ
        //
        // Possible work around for market launches. See http://code.google.com/p/android/issues/detail?id=2373
        // for more details. Essentially, the market launches the main activity on top of other activities.
        // we never want this to happen. Instead, we check if we are the root and if not, we finish.
        //
        // The following code must be run after super.onCreate
        if (!isTaskRoot()) {
            final Intent intent = getIntent();
            final String intentAction = intent.getAction();
            if (intent.hasCategory(Intent.CATEGORY_LAUNCHER) && intentAction != null && intentAction.equals(Intent.ACTION_MAIN)) {
                Log.w(TAG, "Main Activity is not the root.  Finishing Main Activity instead of launching.");
                finish();
                return;
            }
        }

But, the above two techniques doesn't prevent multiple app instances from appearing in task list.

May I know, does anyone of you come across the similar problem? Do you have a good solution to ensure there's only 1 instance of app in the task list?

Cheok Yan Cheng
  • 47,586
  • 132
  • 466
  • 875

1 Answers1

10

Try changing your launchmode to singleTask.

Android docs say:

The system creates the activity at the root of a new task and routes the intent to it. However, if an instance of the activity already exists, the system routes the intent to existing instance through a call to its onNewIntent() method, rather than creating a new one.

Another solution could be singleInstance. Android docs say:

Same as "singleTask", except that the system doesn't launch any other activities into the task holding the instance. The activity is always the single and only member of its task.

Barns
  • 4,850
  • 3
  • 17
  • 31