title: Android deep-linking. Intent doesn't reset when app is opened from history
body: <p>I have a problem regarding Android task and intent management. </p>
  
  <p><strong>Scenario</strong></p>
  
  <ol>
  <li>User gets a push with a deep-link into the app. </li>
  <li>We show a notification putting the URI into the Intent Data. </li>
  <li>User clicks the notification and is taken into the app and redirected to some Feature1Activity described by the deep-link.</li>
  <li>User looks around, and backs out of the app.</li>
  <li>Later, user opens the app from history (long-press home or multitasking button).</li>
  <li>Now the same intent that were used from the notification (with the deep-link in the Intent Data) is used to start the app.</li>
  <li>Hence, user is taken into the Feature1Activity again.</li>
  </ol>
  
  <p><strong>Problem:</strong></p>
  
  <p>Starting the app from history (long-press home or multitasking button) does not reset the Task (which it does when launching from app icon).</p>
  
  <p>I understand that starting an app from history is not supposed to reset the task since it is intended to be used as "get-right-back-where-you-were". However, in my case this is an issue since the launch of the app from a notification is a one time thing.</p>
  
  <p>Anyone else encountered this problem? Anyone know any solution?</p>
  
  <p><strong>More in-depth</strong></p>
  
  <p>The intent inside the PendingIntent is built like this:</p>
  
  <pre><code>Intent intent = new Intent (Intent.ActionView);
  intent.addFlags (Intent.FLAG_ACTIVITY_NEW_TASK);
  intent.addFlags (Intent.FLAG_ACTIVITY_CLEAR_TOP);
  intent.addFlags (Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
  intent.setData (Uri.Parse (DEEP_LINK_URL));
  </code></pre>
  
  <p>I found out about the FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET just this day and really thought that it would git rid of my problem but it made no difference.</p>
  
  <p>There are three activities of interest:</p>
  
  <pre><code>SplashActivity       (main launcher &amp; listener of the deep-linking schema -- this activity just redirects either to login or OverviewActivity)
  OverviewActivity     (authorized user's main activity)
  Feature1Activity     (any feature that the deep-link is pointing to)
  </code></pre>
  
  <p>What happens when the user clicks the notification is that the SplashActivity acts as a listener for the schema and converts the deep-link url to two intents to start up OverviewActivity and Feature1Activity using Activity.startActivities (Intent[]).</p>
  
  <p>When I look at the intent from the notification inside SplashActivity it always contain the deep-link in the Data. </p>
  
  <p><strong>One work around</strong></p>
  
  <p>There is a work around, setting some booleanExtra field to the notification intent (for instance "ignoreWhenLaunchedFromHistory" = true) and then check in SplashActivity before redirecting </p>
  
  <pre><code>boolean fromHistory = (getIntent().getFlags() &amp; FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) == FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY;
  
  if (fromHistory &amp;&amp; getIntent().getBooleanExtra ("ignoreWhenLaunchedFromHistory", false))
      // Don't follow deep-link even if it exists
  else
      // Follow deep-link
  </code></pre>
  
  <p>Except that it hackish and ugly, can you see any problems with this work around?</p>
  
  <p>EDIT: The work around only works when I am responsible for sending the Intent with the deep-link. Since no external source can know about the "ignoreWhenLaunchedFromHistory" extra.</p>