Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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),

  • I was going through an app and the app offered an action to send emails, I selected 'send email' option.
  • My phone's default 'email app' will be picked and its activity (which is declared as singletask) will be opened.
  • 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.

like image 571
nobalG Avatar asked Feb 28 '17 10:02

nobalG


1 Answers

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

  • if your singleTask activity already exists, that copy will be used, with the method onNewIntent() being called
  • if it does not exist, it will be launched as per normal

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:

  • adb dumpsys activity activities

This outputs a lot of text (it may help to reboot the phone before doing this - adb 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.

like image 82
Richard Le Mesurier Avatar answered Oct 01 '22 02:10

Richard Le Mesurier