0

Having this weird problem of multiple instances of my activity getting created even after specifying the launchMode as singleTask. Has anybody faced this issue ? Any pointers will be appreciated.

Below is how i have declared my activity in the manifest:

    <activity
    android:name="com.test.TestActivity"
        android:launchMode="singleTask">
     <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

            <intent-filter>
                <action android:name="android.intent.action.SEND" />
                <action android:name="android.intent.action.SEND_MULTIPLE" />

                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="*/*" />
            </intent-filter>
</activity>

And this is how i am launching my activity while it is still in foreground:

Intent sendIntent = new Intent();
        sendIntent.setAction(Intent.ACTION_SEND);
        sendIntent.putExtra(Intent.EXTRA_TEXT, sharedText);
        sendIntent.setType("text/plain");
        activity.startActivity(Intent.createChooser(sendIntent, "Share"));

Android version that i am testing it on is 5.1.1

2 Answers2

1

Try using singleInstance if you want to get rid of your activity's multiple instances.

    <activity
        android:name=".MainActivity"
        android:launchMode="singleInstance">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

EXTRA NOTE:

singleTask :- A new task will always be created and a new instance will be pushed to the task as the root. However, if any activity instance exists in any tasks, the system routes the intent to that activity instance through the onNewIntent() method call. In this mode, activity instances can be pushed to the same task. This mode is useful for activities that act as the entry points.

singleInstance:- Same as singleTask, except that the no activities instance can be pushed into the same task of the singleInstance’s. Accordingly, the activity with launch mode is always in a single activity instance task. This is a very specialized mode and should only be used in applications that are implemented entirely as one activity.

Copied from : https://stackoverflow.com/a/36520016/3669559

Oğuzhan Döngül
  • 7,856
  • 4
  • 38
  • 52
  • @ADM added detailed explanation for you. – Oğuzhan Döngül May 26 '18 at 11:21
  • @OğuzhanDöngül Although singleTask should work just fine, but i have tried singleInstance also and it didn't help. – Varun Sharma May 26 '18 at 11:24
  • @OğuzhanDöngül : An Activity with singleTask launchMode is allowed to have only one instance in the system (a.k.a. Singleton). If there is an existed Activity instance in the system, the whole Task hold the instance would be moved to top while Intent would be delivered through onNewIntent() method. Otherwise, new Activity would be created and placed in the proper Task. – Bhavesh Rangani May 26 '18 at 11:26
  • 1
    NO need to copy-paste the documentation here . `singleTask` and `singleInstace` are almost same just one difference is there. OP's question is not complete cause we do not know how the activity is launching at runtime . – ADM May 26 '18 at 11:27
  • @OğuzhanDöngül I have added code snippet to my original question. Please have a look at it. – Varun Sharma May 26 '18 at 18:00
0

So it seems the issue is with device. On emulator and other devices with same android version singleTask works just fine.

  • 1
    What device were you testing on? There is a long-standing Android bug that will create multiple instances of the app if it is initially launched from the installer or from an IDE. To test if this is the problem, you should install your app, then force stop the app, then launch it from the HOME screen as usual. It should now behave properly. – David Wasser May 28 '18 at 17:34
  • The device i was testing on is Kindle Fire 7. And i will surely test it out the way u suggested. – Varun Sharma May 29 '18 at 05:39