title: Lollipop: making my activity stay in the task that launched the share intent
body: <p>“My First App” has an activity that handles a “share” intent.  Its activity in <code>AndroidManifest.xml</code> looks like this: </p>
  
  <pre><code>    &lt;activity
          android:name="com.example.foo.myfirstapp.MainActivity"
          android:label="@string/app_name"&gt;
          &lt;intent-filter&gt;
            &lt;action android:name="android.intent.action.SEND"/&gt;
            &lt;category android:name="android.intent.category.DEFAULT"/&gt;
            &lt;data android:mimeType="image/jpeg"/&gt;
          &lt;/intent-filter&gt;
  
      &lt;/activity&gt;
  </code></pre>
  
  <p>In KitKat, sharing an image from the album to “My First App” causes MainActivity to be part of the album’s task.  This is the desired behavior. </p>
  
  <p>In Lollipop, sharing an image from the album to “My First App” causes a new instance of “My First App” to be launched. If I look at the overview screen, the album task is there...and then there's a separate entry for "My First App".  If I share another image, I wind up with two instances of "My First App"...etc.</p>
  
  <p><b>Question: How do I make Lollipop process the share intent in the same way as KitKat?</b></p>
  
  <p>Here's what I've done: 
  I notice that the intents sent from the Album have different flags set depending on the OS. (I got these using <code>getIntent()</code> and looking at <code>mFlags</code>.)</p>
  
  <p>Kitkat: <code>0x80001</code> (524289): FLAG_GRANT_READ_URI_PERMISSION, FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET</p>
  
  <p>Lollipop: <code>0x18080001</code> (403177473): FLAG_GRANT_READ_URI_PERMISSION, FLAG_ACTIVITY_MULTIPLE_TASK, FLAG_ACTIVITY_NEW_DOCUMENT, FLAG_ACTIVITY_NEW_TASK</p>
  
  <p>From reading <a href="http://developer.android.com/reference/android/content/Intent.html" rel="nofollow">http://developer.android.com/reference/android/content/Intent.html</a>, it seems that these last three flags are causing the problem. Specifically</p>
  
  <blockquote>
    <p>When paired with FLAG_ACTIVITY_MULTIPLE_TASK both of these behaviors (FLAG_ACTIVITY_NEW_DOCUMENT or FLAG_ACTIVITY_NEW_TASK) are modified to skip the search for a matching task and unconditionally start a new task. </p>
  </blockquote>
  
  <p>I’ve been attempting to “override” these flags by specifying <code>android:launchMode</code> and <code>android:documentLaunchMode</code> in the activity in <code>AndroidManifest.xml</code> without success.</p>
  
  <p>From <a href="http://developer.android.com/reference/android/R.attr.html#documentLaunchMode" rel="nofollow">http://developer.android.com/reference/android/R.attr.html#documentLaunchMode</a>, using documentLaunchMode “never” seems promising, since</p>
  
  <blockquote>
    <p>This activity will not be launched into a new document even if the Intent contains Intent.FLAG_ACTIVITY_NEW_DOCUMENT. This gives the activity writer ultimate control over how their activity is used.
    but this didn't work. </p>
  </blockquote>
  
  <p>I also considered <code>android:taskAffinity</code>, but there doesn’t seem to be a way to say “please prefer whatever task launched you”.</p>