androidlaunchmode

SingleTask activity, but opened through two different applications


This image was quite helpful for understanding the functionality offered by the launhmode singleTask, taken from here

enter image description here however, I understood this in case of the same application, I am having issues understanding what if both tasks belong to two different Applications

Confusing Scenario(fictional),

The main thing is this time, the tasks/stacks belong to two different apps.


Solution

  • Even though you are using 2 different applications, it will work in the expected way:

    More technically, reproducing the definition from your link:

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


    This can easily be verified by making an activity a target for sharing text and singleTask in the manifest:

        <activity
            android:name=".MainActivity"
            android:launchMode="singleTask">
            <intent-filter>
                <action android:name="android.intent.action.SEND" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="text/plain" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.SEND_MULTIPLE" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="image/*" />
            </intent-filter>
        </activity>
    

    Now add some logging to the onCreate() and onNewIntent() methods and do some scenario testing.


    Something I found particularly useful when testing the various launchmodes is the following ADB command:

    This outputs a lot of text (it may help to reboot the phone before doing this - adb shell reboot) showing details of the activity task stacks. This can be used to show you that your singleTask activity "rehomes" itself as it gets launched via different applications.


    As for the question about the emails, I think that will depend on which email client you are using, but I would hope that they handle the onNewIntent() method correctly, and save the current draft before displaying your new email.