This image was quite helpful for understanding the
functionality offered by the launhmode singleTask
, taken from 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),
While I was typing my email content, I switched to some chat app and the app gets crashed and offered me an option to report an issue over email to the developer, Now when I will select 'Report' , my email app(which is the same default email app ) will be opened.
Now as the Email app's root activity is singletask, will my content which I wrote will be visible to me?
The main thing is this time, the tasks/stacks belong to two different apps.
Even though you are using 2 different applications, it will work in the expected way:
singleTask
activity already exists, that copy will be used, with the method onNewIntent()
being calledMore 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:
adb shell dumpsys activity activities
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.